一个简单的缩放 拖动实现

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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值