一个简单的缩放 拖动实现

Posted Sun_TTTT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的缩放 拖动实现相关的知识,希望对你有一定的参考价值。

public class Gesture 
    private boolean isFirst;
    private int mode;
    private static final int DRAG = 1;
    private static final int MOVE = 2;
    private static final int NONE = 0;
    private float oldDis;
    private float newDis;
    private IGesture gesture;
    private float oldX;
    private float oldY;
    private float newX;
    private float newY;
    private boolean result;
    private int status;
    private static final int LEFT = 1;
    private static final int BOTTOM = 4;
    private static final int UP = 3;
    private static final int RIGHT = 2;
    private static final int ZOOMIN = 5;
    private static final int ZOOMOUT = 6;


    public Gesture(IGesture gesture) 
        this.gesture = gesture;
    

    public boolean gesture(MotionEvent event) 
        switch (event.getAction() & MotionEvent.ACTION_MASK) 
            case MotionEvent.ACTION_DOWN:
                mode = DRAG;
                oldX = event.getRawX();
                oldY = event.getRawY();
                result = true;
                break;
            case MotionEvent.ACTION_UP:
                if (mode == MOVE) 

                 else 
                    switch (status) 
                        case 0:

                            break;
                        case LEFT:
                            gesture.stopLeft();
                            break;
                        case RIGHT:
                            gesture.stopRight();
                            break;
                        case BOTTOM:
                            gesture.stopBottom();
                            break;
                        case UP:
                            gesture.stopUp();
                            break;

                    
                    isFirst = true;


                
                mode=NONE;
                result = true;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                mode = MOVE;
                oldDis = spacing(event);
                result = true;
                break;
            case MotionEvent.ACTION_POINTER_UP:
                switch (status) 
                    case ZOOMIN:
                        gesture.stopZoomIn();
                        break;
                    case ZOOMOUT:
                        gesture.stopZoomOut();
                        break;

                
                mode = NONE;
                result = true;
                break;
            case MotionEvent.ACTION_MOVE:
                if (isFirst) 


                    switch (mode) 
                        case NONE:

                            break;
                        case DRAG:
                            newX = event.getRawX();
                            newY = event.getRawY();

                            if (newX - oldX > 0 && Math.abs(newX - oldX) > Math.abs(newY - oldY)) 
                                //toRight
                                gesture.moveRight();
                                status = RIGHT;


                             else if (newX - oldX < 0 && Math.abs(newX - oldX) > Math.abs(newY - oldY)) 
                                //toLeft
                                gesture.moveLeft();
                                status = LEFT;


                             else if (newY - oldY > 0 && Math.abs(newY - oldY) > Math.abs(newX - oldX)) 
                                //toBottom
                                gesture.moveBottom();
                                status = BOTTOM;

                             else if (newY - oldY < 0 && Math.abs(newY - oldY) > Math.abs(newX - oldX)) 
                                gesture.moveUp();
                                status = UP;

                            


                            break;
                        case MOVE:
                            newDis = spacing(event);
                            if (newDis - oldDis > 1) 
                                status = ZOOMOUT;
                                gesture.zoomOut();
                             else if (oldDis - newDis > 1) 
                                status = ZOOMIN;
                                gesture.zoomIn();
                            
                            break;


                    
                    isFirst = false;
                    result = true;
                    break;
                 else 
                    //只需要接受第一次move即可 后面的move都被忽略
                
        
        return result;
    

    private float spacing(MotionEvent event) 
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    

IGesture

public interface IGesture 
    void moveUp();
    void moveBottom();
    void moveLeft();
    void moveRight();
    void zoomIn();
    void zoomOut();
    void stopUp();
    void stopBottom();
    void stopLeft();
    void stopRight();
    void stopZoomIn();
    void stopZoomOut();

以上是关于一个简单的缩放 拖动实现的主要内容,如果未能解决你的问题,请参考以下文章

使用 Android 内置的手势监听器和缩放监听器实现捏缩放和拖动

Android实现图片的缩放和拖动

正交相机下实现滚轮按钮拖动,滚动滚轮缩放的功能

如何通过拖动视图边缘/角来实现缩放,如应用官方照片应用裁剪控制?

简单实现svg的拖拽和缩放

如何实现拖动修改网页中div模块大小