Android放大缩小图片手势操作2014-10-19 14:12:25

( 2人已投票,[高质量] )
分享:
31.3K

Android中经常会用到对图片放大缩小移动等操作,多点触碰操作都是很常见的处理方式,现面我就来实现一下

如何动态的显示一张图片,并给该图片添加一些监听操作,比如移动、多点触碰、放大缩小等内容

首先上一张效果图:

Android放大缩小图片手势操作

布局文件:

//main_activity.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:id="@+id/linearLayout1"
  android:weightSum="1">
  <ImageView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/imag1"
  android:scaleType="matrix"
  ></ImageView>
</LinearLayout>

//matrix属性是关键


//MainActivity

public class MainActivity extends Activity implements OnTouchListener {//implements OnTouchListener {
private ImageView imgview;
private Matrix matrix=new Matrix();
private Matrix savedMatrix=new Matrix();
                                                                                               
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
                                                                                               
//private GestureDetector gesture;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
imgview=(ImageView)this.findViewById(R.id.imag1);
imgview.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.chopper));
imgview.setOnTouchListener(this);
imgview.setLongClickable(true);
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getActionMasked()==MotionEvent.ACTION_POINTER_UP)
Log.d("Infor", "多点操作");
switch(event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
matrix.set(imgview.getImageMatrix());
savedMatrix.set(matrix);
start.set(event.getX(),event.getY());
Log.d("Infor", "触摸了...");
mode=DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN: //多点触控
oldDist=this.spacing(event);
if(oldDist>10f){
Log.d("Infor", "oldDist"+oldDist);
savedMatrix.set(matrix);
midPoint(mid,event);
mode=ZOOM;
}
break;
case MotionEvent.ACTION_POINTER_UP:
mode=NONE;
break;
case MotionEvent.ACTION_MOVE:
if(mode==DRAG){     //此实现图片的拖动功能...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX()-start.x, event.getY()-start.y);
}
else if(mode==ZOOM){// 此实现图片的缩放功能...
float newDist=spacing(event);
if(newDist>10){
matrix.set(savedMatrix);
float scale=newDist/oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);      
}
}
break;
}
imgview.setImageMatrix(matrix);
return false;
}
}

//onTouch中多种动作处理是关键:移动、放大、缩小


已测试,如果还有不明白的,源码包下载,自行领会或修改使用:

Android放大缩小图片手势操作 Android_image_Matrix.zip [登录雪炭网后可见]






头像

snowcoal
  • Android
  • 放大缩小
  • 多点触碰

收藏到我的私密空间

标题:Android放大缩小图片手势操作

作者:柳岸花明

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限