android关于图片缩放

网上有许多关于图片缩放的demo,本人都感觉不怎么好用,最近在github看到了 一个简单的支持多指缩放图片的Android View类 gesture-imageview (地址:https://github.com/jasonpolites/gesture-imageview),感觉还挺好用的,现在写个demo方便以后用于调用

第一步:添加库,推荐直接下载zip,导入工程后,直接将main里的com.polites.android包直接复制到自己的工程中,方便自己以后修改

第二步:由于我只需用到GestureImageView类就可以了,所以本例子只调用了这个类

以下是代码:MainActivity.java

package com.example.android_imageview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.polites.android.GestureImageView;

public class MainActivity extends Activity implements OnClickListener{
    protected GestureImageView view;
    Button btn_save,btn_close;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = (GestureImageView) findViewById(R.id.gestureimage_image);
        //本例子只用本地图片作为示例图片,图片可以任意改
        view.setImageResource(R.drawable.ic_launcher);
    }

    @Override
    public void onClick(View arg0) {
        if(arg0.getId() == R.id.btn_save){
            //保存图片的method
            
        }else if(arg0.getId() == R.id.btn_close){
            //关闭Activity
            finish();
        }
        
    }

}


布局文件:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:gesture-image="http://schemas.polites.com/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">
    
    <com.polites.android.GestureImageView
        android:id="@+id/gestureimage_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_centerInParent="true"
        gesture-image:min-scale="0.75"
        gesture-image:max-scale="10.0"/>
    <Button 
        android:id="@+id/btn_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="save"/>
    <Button 
        android:id="@+id/btn_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="close"/>

</RelativeLayout>

 

注意:RelativeLayout中的 xmlns:gesture-image=http://schemas.polites.com/android必须要添加,不然得话会报错,以下三个属性为控制图片缩放的

gesture-image:min-scale 缩放最小值
gesture-image:max-scale 缩放最大值
gesture-image:strict  是否精确

附加一句:GestureImageView使用方式和 ImageView 的使用方式是一样的。

各位也可看一下源码,更清楚一点

posted on 2014-07-07 20:12  凡人柯  阅读(489)  评论(0编辑  收藏  举报