本文实例为大家分享了AndroID手势控制实现缩放、移动图片的方法,供大家参考,具体内容如下
新建一个触摸监听器类
package com.liyong.btprinter; import androID.graphics.Matrix; import androID.graphics.PointF; import androID.util.floatMath; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OntouchListener; import androID.Widget.ImageVIEw; public class MulitPointtouchListener implements OntouchListener { private static final String TAG = "touch"; // These matrices will be used to move and zoom image Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); // We can be in one of these 3 states 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; @OverrIDe public boolean ontouch(VIEw v,MotionEvent event) { ImageVIEw vIEw = (ImageVIEw) v; // Log.e("vIEw_wIDth",// vIEw.getimageMatrix()..toString()+"*"+v.getWIDth()); // Dump touch event to log dumpEvent(event); // Handle touch events here... switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: matrix.set(vIEw.getimageMatrix()); savedMatrix.set(matrix); start.set(event.getX(),event.getY()); //Log.d(TAG,"mode=DRAG"); mode = DRAG; //Log.d(TAG,"mode=NONE"); break; case MotionEvent.ACTION_POINTER_DOWN: olddist = spacing(event); //Log.d(TAG,"olddist=" + olddist); if (olddist > 10f) { savedMatrix.set(matrix); mIDPoint(mID,event); mode = ZOOM; //Log.d(TAG,"mode=ZOOM"); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; //Log.e("vIEw.getWIDth",vIEw.getWIDth() + ""); //Log.e("vIEw.getHeight",vIEw.getHeight() + ""); 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); //Log.d(TAG,"newdist=" + newdist); if (newdist > 10f) { matrix.set(savedMatrix); float scale = newdist / olddist; matrix.postscale(scale,scale,mID.x,mID.y); } } break; } vIEw.setimageMatrix(matrix); return true; // indicate event was handled } private voID dumpEvent(MotionEvent event) { String names[] = { "DOWN","UP","MOVE","CANCEL","OUTSIDE","POINTER_DOWN","POINTER_UP","7?","8?","9?" }; StringBuilder sb = new StringBuilder(); int action = event.getAction(); int actionCode = action & MotionEvent.ACTION_MASK; sb.append("event ACTION_").append(names[actionCode]); if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) { sb.append("(pID ").append( action >> MotionEvent.ACTION_POINTER_ID_SHIFT); sb.append(")"); } sb.append("["); for (int i = 0; i < event.getPointerCount(); i++) { sb.append("#").append(i); sb.append("(pID ").append(event.getPointerID(i)); sb.append(")=").append((int) event.getX(i)); sb.append(",").append((int) event.getY(i)); if (i + 1 < event.getPointerCount()) sb.append(";"); } sb.append("]"); //Log.d(TAG,sb.toString()); } 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); } }
编写一个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:ID="@+ID/privIEwimage" androID:layout_height="fill_parent" androID:layout_gravity="center" androID:scaleType="matrix" > </ImageVIEw> </linearLayout>
注意其中的: androID:scaleType="matrix"
privIEwimage.setimageBitmap(ActivityPrevIEwImage.click_bitmap); privIEwimage=(ImageVIEw)findVIEwByID(R.ID.privIEwimage); privIEwimage.setontouchListener(new MulitPointtouchListener());
以上就是AndroID手势控制缩放移动图片的全部代码,希望对大家的学习有所帮助。
总结以上是内存溢出为你收集整理的Android手势控制实现缩放、移动图片全部内容,希望文章能够帮你解决Android手势控制实现缩放、移动图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)