缩放事件不适用于android中的ImageView

时间:2014-12-19 09:36:00

标签: android android-imageview ontouchlistener pinchzoom

我在android中制作了一个重叠两个图像的演示。从图库中选择任何图像并在其上放置任何贴纸,我们可以缩放和拖动并移动贴纸。我已经使移动时刻事件成功,但缩放活动不起作用,请帮帮我。我的代码如下:

imageDetail.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility") @Override
        public boolean onTouch(View v, MotionEvent event) {

            ImageView view = (ImageView) v;
            System.out.println("matrix=" + savedMatrix.toString());


            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                savedMatrix.set(matrix);
                startPoint.set(event.getX(), event.getY());
                mode = DRAG;
                break;

            case MotionEvent.ACTION_POINTER_DOWN:

                oldDist = spacing(event);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(midPoint, event);
                    mode = ZOOM;
                }
                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() - startPoint.x,
                            event.getY() - startPoint.y);
                } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;
                        matrix.postScale(scale, scale, midPoint.x, midPoint.y);
                    }
                }
                break;
            }
            view.setImageMatrix(matrix);

            return true;
        }

        @SuppressLint("FloatMath")
        public 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);
        }

        public 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);
        }
    });

0 个答案:

没有答案