返回顶部

收藏

Android手势缩放图片以及图片黏贴在手指随手势移动

更多

一个Android手势缩放图片的工具类;同时,此类还实现另外一个功能:当手指按在触屏上移动时候,图片“黏贴”在手指上随手指移动而整体移动。

具体使用方法可以是这样:先new一个此类的实例,然后在ImageView的方法setOnTouchListener(new ImageViewOnMultiTouchListener());

例如:

    ImageViewOnMultiTouchListener listener=new ImageViewOnMultiTouchListener();  
            ImageView mImageView=(ImageView)findViewById(R.id.imageView);  
            mImageView.setOnTouchListener(listener);  

ImageViewOnMultiTouchListener.java全部源代码:

    public class ImageViewOnMultiTouchListener implements View.OnTouchListener {  

            private final int NONE = 0;  
            private final int MOVE = 1;  
            private final int ZOOM = 2;  
            private final int DRAG = 3;  

            private int mode = NONE;  
            private Matrix matrix = new Matrix();  
            private Matrix savedMatrix = new Matrix();  
            private PointF start = new PointF();  
            private PointF mid = new PointF();  
            private float oldDistance = 0;  

            @Override  
            public boolean onTouch(View v, MotionEvent event) {  

                ImageView view = (ImageView) v;  

                switch (event.getAction() & MotionEvent.ACTION_MASK) {  
                case MotionEvent.ACTION_DOWN:  
                    matrix.set(view.getImageMatrix());  
                    savedMatrix.set(matrix);  
                    start.set(event.getX(), event.getY());  

                    mode = DRAG;  

                    break;  

                case MotionEvent.ACTION_POINTER_DOWN:  
                    oldDistance = spacing(event);  
                    if (oldDistance > 5f) {  
                        savedMatrix.set(matrix);  
                        midPoint(mid, 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() - start.x, event.getY()  
                                - start.y);  
                    } else if (mode == ZOOM) {  
                        float newDist = spacing(event);  
                        if (newDist > 5f) {  
                            matrix.set(savedMatrix);  
                            float scale = newDist / oldDistance;  
                            matrix.postScale(scale, scale, mid.x, mid.y);  
                        }  
                    }  

                    break;  
                }  

                view.setImageMatrix(matrix);  
                view.setScaleType(ImageView.ScaleType.MATRIX);  
                view.setPadding(3, 5, 3, 5);  

                return true;  
            }  

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

标签:java

收藏

0人收藏

支持

0

反对

0

发表评论