【Android__WebView】多點觸摸實現縮放和拖拽圖片


http://www.4byte.cn/question/634906/android-webview-can-t-be-scrolled-after-being-set-to-a-touch-listener.html html


在MotionEvent中有兩個事件值得注意一下,分別是ACTION_DOWN和ACTION_POINTER_DOWN。java

         ACTION_DOWN:這個很好理解,當屏幕檢測到有手指按下以後就觸發到這個事件。android

        ACTION_POINTER_DOWN:這個是實現多點的關鍵,當屏幕檢測到有多個手指同時按下以後,就觸發了這個事件。web

經過getAction()能夠得到當前屏幕的事件類型:ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_CANCEL。可是若是像得到如上的ACTION_POINTER_DOWN,則得和ACTION_MASK相與才能獲得ACTION_POINTER_DOWN事件。app

@Override
        public boolean onTouch(View v, MotionEvent event) {
                ImageView view = (ImageView) v;
                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");
                        }// ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_CANCEL.
                        break;
                case MotionEvent.ACTION_UP:
                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);
                                //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);
}

將以上代碼寫到MulitPointTouchListener .java中,而後對你相應的圖片進行OnTouchListener
好比:imageView.setOnTouchListener(new MulitPointTouchListener ());xml中要將圖片的縮放格式改爲Matrix
ide

相關文章
相關標籤/搜索