时间:2021-07-19 08:00:31 | 栏目:Android代码 | 点击:次
先来一张效果图
TIM图片.gif
上图中灰色的一块是ImageView控件,ImageView中的图片进行左右上下移动,以及双指缩放。
对于android控件的缩放移动,点这里----android控件的缩放,移动
布局layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/mywhite" android:gravity="center_horizontal" android:fitsSystemWindows="true"> <ImageView android:background="@color/light_gery" android:scaleType="matrix" android:src="@drawable/ic_sure" android:id="@+id/hair_iv" android:layout_marginTop="50dp" android:layout_width="300dp" android:layout_height="300dp"/> </LinearLayout>
先看关于手势触摸的判断,需要判断用户是单指触摸还是双指,分别在OnTounch的监听事件中判断。
注:先重写onLongClick()方法,不然会影响OnTouch里面的触摸监听。
private static final int NONE = 0; private static final int DRAG = 1; private static final int ZOOM = 2; private int mode = NONE; hairIv.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { return true; } }); hairIv.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: //单点触控 mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: //多点触控 break; case MotionEvent.ACTION_MOVE: // 手指滑动 if (mode == DRAG) { // 是一个手指拖动 Log.d(TAG, "mode = DRAG" ); } else if (mode == ZOOM) { // 两个手指滑动 Log.d(TAG, "mode = ZOOM" ); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: // 手指放开事件 mode = NONE; break; } return true; } });
这些写上之后就可以先看一下单指,双指的触摸事件有没有被正确处理
通过Android提供的Matrix类对图像进行处理,关于Matrix的原理可以看一下Android官方文档Matrix部分,也可以找一些其他的博客,这里只看使用。
定义要用到的变量
private Matrix matrix = new Matrix(); private Matrix savedMatrix = new Matrix(); // 第一个按下的手指的点 private PointF startPoint = new PointF(); // 两个按下的手指的触摸点的中点 private PointF midPoint = new PointF(); // 初始的两个手指按下的触摸点的距离 private float oriDis = 1f;
在onTouch中完善
@Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; final int x = (int) event.getRawX(); final int y = (int) event.getRawY(); Log.d(TAG, "onTouch: x= " + x + "y=" + y); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: //单点触控 matrix.set(view.getImageMatrix()); savedMatrix.set(matrix); startPoint.set(event.getX(), event.getY()); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: //多点触控 oriDis = distance(event); if (oriDis > 10f) { savedMatrix.set(matrix); midPoint = midPoint(event); mode = ZOOM; } break; case MotionEvent.ACTION_MOVE: // 手指滑动事件 if (mode == DRAG) { // 是一个手指拖动 matrix.set(savedMatrix); matrix.postTranslate(event.getX() - startPoint.x, event.getY() - startPoint.y); } else if (mode == ZOOM) { // 两个手指滑动 float newDist = distance(event); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oriDis; matrix.postScale(scale, scale, midPoint.x, midPoint.y); } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: // 手指放开事件 mode = NONE; break; } view.setImageMatrix(matrix); return true; }
其中用到的计算位置和距离的方法
/** * 计算两个手指头之间的中心点的位置 * x = (x1+x2)/2; * y = (y1+y2)/2; * * @param event 触摸事件 * @return 返回中心点的坐标 */ private PointF midPoint(MotionEvent event) { float x = (event.getX(0) + event.getX(1)) / 2; float y = (event.getY(0) + event.getY(1)) / 2; return new PointF(x, y); } /** * 计算两个手指间的距离 * * @param event 触摸事件 * @return 放回两个手指之间的距离 */ private float distance(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return (float) Math.sqrt(x * x + y * y);//两点间距离公式 }
public class ImageActivity extends AppCompatActivity{ @BindView(R.id.hair_iv) ImageView hairIv; private static final int NONE = 0; private static final int DRAG = 1; private static final int ZOOM = 2; private int mode = NONE; private Matrix matrix = new Matrix(); private Matrix savedMatrix = new Matrix(); // 第一个按下的手指的点 private PointF startPoint = new PointF(); // 两个按下的手指的触摸点的中点 private PointF midPoint = new PointF(); // 初始的两个手指按下的触摸点的距离 private float oriDis = 1f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); ButterKnife.bind(this); hairIv.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { return true; } }); hairIv.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; final int x = (int) event.getRawX(); final int y = (int) event.getRawY(); Log.d(TAG, "onTouch: x= " + x + "y=" + y); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: //单点触控 matrix.set(view.getImageMatrix()); savedMatrix.set(matrix); startPoint.set(event.getX(), event.getY()); mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: //多点触控 oriDis = distance(event); if (oriDis > 10f) { savedMatrix.set(matrix); midPoint = midPoint(event); mode = ZOOM; } break; case MotionEvent.ACTION_MOVE: // 手指滑动事件 if (mode == DRAG) { // 是一个手指拖动 matrix.set(savedMatrix); matrix.postTranslate(event.getX() - startPoint.x, event.getY() - startPoint.y); } else if (mode == ZOOM) { // 两个手指滑动 float newDist = distance(event); if (newDist > 10f) { matrix.set(savedMatrix); float scale = newDist / oriDis; matrix.postScale(scale, scale, midPoint.x, midPoint.y); } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: // 手指放开事件 mode = NONE; break; } view.setImageMatrix(matrix); return true; } }); } /** * 计算两个手指头之间的中心点的位置 * x = (x1+x2)/2; * y = (y1+y2)/2; * * @param event 触摸事件 * @return 返回中心点的坐标 */ private PointF midPoint(MotionEvent event) { float x = (event.getX(0) + event.getX(1)) / 2; float y = (event.getY(0) + event.getY(1)) / 2; return new PointF(x, y); } /** * 计算两个手指间的距离 * * @param event 触摸事件 * @return 放回两个手指之间的距离 */ private float distance(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return (float) Math.sqrt(x * x + y * y);//两点间距离公式 } }
总结