关闭

[转载]Android通过手势(多点)缩放和拖拽图片

689人阅读 评论(0) 收藏 举报

原文地址:Android通过手势(多点)缩放和拖拽图片    作者:望夜

在MotionEvent中有两个事件值得注意一下,分别是ACTION_DOWN和ACTION_POINTER_DOWN。

       ACTION_DOWN:这个很好理解,当屏幕检测到有手指按下之后就触发到这个事件。
       ACTION_POINTER_DOWN:这个是实现多点的关键,当屏幕检测到有多个手指同时按下之后,就触发了这个事件。
通过getAction()可以获得当前屏幕的事件类型:ACTION_DOWN, ACTION_MOVE, ACTION_UP, orACTION_CANCEL。但是如果像获得如上的ACTION_POINTER_DOWN,则得和ACTION_MASK相与才能得到ACTION_POINTER_DOWN事件。

       所以我们运用一个switch-case结构就可以实现对图片进行拖拽或缩放,而缩放的只要思想是在移动的过程中计算两个点之间的距离,然后再进行缩放。

      上代码:
  1.       @Override
  2.        publicboolean onTouch(View v, MotionEvent event) {
  3.              ImageViewview = (ImageView) v;
  4.             dumpEvent(event);
  5.              // Handletouch events here...
  6.              switch(event.getAction() & MotionEvent.ACTION_MASK){
  7.              caseMotionEvent.ACTION_DOWN:
  8.                  matrix.set(view.getImageMatrix());
  9.                  savedMatrix.set(matrix);
  10.                  start.set(event.getX(), event.getY());
  11.                   //Log.d(TAG,"mode=DRAG");
  12.                   mode =DRAG;
  13.                   //Log.d(TAG,"mode=NONE");
  14.                  break;
  15.              caseMotionEvent.ACTION_POINTER_DOWN:
  16.                   oldDist =spacing(event);
  17.                   //Log.d(TAG,"oldDist=" + oldDist);
  18.                   if (oldDist> 10f) {
  19.                        savedMatrix.set(matrix);
  20.                        midPoint(mid,event);
  21.                        mode =ZOOM;
  22.                        //Log.d(TAG,"mode=ZOOM");
  23.                   }//ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_CANCEL.
  24.                  break;
  25.              caseMotionEvent.ACTION_UP:
  26.              caseMotionEvent.ACTION_POINTER_UP:
  27.                   mode =NONE;
  28.                  break;
  29.              caseMotionEvent.ACTION_MOVE:
  30.                   if (mode ==DRAG) {
  31.                        //...
  32.                        matrix.set(savedMatrix);
  33.                        matrix.postTranslate(event.getX()- start.x, event.getY()
  34.                                   -start.y);
  35.                   } else if(mode == ZOOM) {
  36.                        float newDist= spacing(event);
  37.                        //Log.d(TAG,"newDist=" + newDist);
  38.                        if (newDist> 10f) {
  39.                             matrix.set(savedMatrix);
  40.                              float scale= newDist / oldDist;
  41.                             matrix.postScale(scale, scale, mid.x, mid.y);
  42.                        }
  43.                   }
  44.                  break;
  45.             }

  46.             view.setImageMatrix(matrix);
  47.              returntrue; // indicate event was handled
  48.        }

  49.        private voiddumpEvent(MotionEvent event) {
  50.              Stringnames[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
  51.                        "POINTER_DOWN","POINTER_UP", "7?", "8?", "9?" };
  52.             StringBuilder sb = new StringBuilder();
  53.              int action= event.getAction();
  54.              intactionCode = action &MotionEvent.ACTION_MASK;
  55.             sb.append("event ACTION_").append(names[actionCode]);
  56.              if(actionCode == MotionEvent.ACTION_POINTER_DOWN
  57.                        || actionCode== MotionEvent.ACTION_POINTER_UP) {
  58.                  sb.append("(pid ").append(
  59.                              action>>MotionEvent.ACTION_POINTER_ID_SHIFT);
  60.                  sb.append(")");
  61.             }
  62.             sb.append("[");
  63.              for (int i= 0; i < event.getPointerCount(); i++) {
  64.                  sb.append("#").append(i);
  65.                  sb.append("(pid").append(event.getPointerId(i));
  66.                  sb.append(")=").append((int)event.getX(i));
  67.                  sb.append(",").append((int)event.getY(i));
  68.                   if (i + 1< event.getPointerCount())
  69.                        sb.append(";");
  70.             }
  71.             sb.append("]");
  72.             //Log.d(TAG, sb.toString());
  73.        }
复制代码
求两点间距离:
  1. private float spacing(MotionEvent event) {
  2.              float x =event.getX(0) - event.getX(1);
  3.              float y =event.getY(0) - event.getY(1);
  4.              returnFloatMath.sqrt(x * x + y * y);
  5.        }
复制代码

求两点间中点:
  1.        private voidmidPoint(PointF point, MotionEvent event) {
  2.              float x =event.getX(0) + event.getX(1);

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:74437次
    • 积分:842
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:23篇
    • 译文:0篇
    • 评论:19条
    文章分类
    最新评论