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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

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

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: Android 再按一次退出应用的代码

下一篇:Android开发中常用的工具类整理