加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:29:58 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ImageViewOnMultiTouchListener listener=new ImageViewOnMultiTouchListener(); ImageView mImageView=(ImageView)findViewById(R.id.imageView); mI

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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

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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读