Android手势识别类,GestureDetector,ScaleGestureDetector

目录

1. GestureDetector

(1) 包含方法

(2) 使用GestureDetector

2. ScaleGestureDetector

(1) 包含方法

(2) 使用ScaleGestureDetector

3. 混合使用


Android程序使用中会有很多的手势,双击、长按、滑动、缩放等,我们可以通过手势识别类GestureDetectorScaleGestureDetector进行识别。

1. GestureDetector

(1) 包含方法

onDown  按下触发。

onShowPress  按下但尚未松开或移动手指时调用。

onSingleTapUp  单点后抬起触发。

onScroll  滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。

onLongPress  长按触发。

onFling  迅速滑动后抬起手指时触发。motionEvent:按下事件;motionEvent1:抬起事件;vx:在x轴上的速度;vy:在y轴上的速度。

(2) 使用GestureDetector

可使用OnGestureListenerSimpleOnGestureListener作为参数;参数使用OnGestureListener需要实现全部方法;参数使用SimpleOnGestureListener只需要实现任意数量的方法。

//参数使用OnGestureListener需要实现全部方法
GestureDetector gestureDetector=new GestureDetector( context , new GestureDetector.OnGestureListener() {
    public boolean onDown(MotionEvent motionEvent) {
        //按下触发。
        return false;
    }
    public void onShowPress(MotionEvent motionEvent) {
        //按下但尚未松开或移动手指时调用。
    }
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        //单点后抬起触发。
        return false;
    }
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float distanceX, float distanceY) {
        //滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。
        return false;
    }
    public void onLongPress(MotionEvent motionEvent) {
        //长按触发。
    }
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float vx, float vy) {
        //迅速滑动后抬起手指时触发。motionEvent:按下事件;motionEvent1:抬起事件;vx:在x轴上的速度;vy:在y轴上的速度。
        return false;
    }
});


imageView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //ix= motionEvent.getX();
        //iy=motionEvent.getY();
        //传入触碰事件
        gestureDetector.onTouchEvent(motionEvent);
        return true;
    }
});
//参数使用SimpleOnGestureListener只需要实现任意数量的方法
GestureDetector gestureDetector = new GestureDetector( context , new GestureDetector.SimpleOnGestureListener() {
    public boolean onScroll( MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        //滑动触发。 motionEvent:按下事件;motionEvent1:当前事件;distanceX:在x轴上的滑动距离;distanceY:在y轴上的滑动距离。
        return false;
    }
});


imageView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //ix= motionEvent.getX();
        //iy=motionEvent.getY();
        //传入触碰事件
        gestureDetector.onTouchEvent(motionEvent);
        return true;
    }
});

2. ScaleGestureDetector

(1) 包含方法

onScale  缩放时调用。

onScaleBegin  缩放开始时调用。

onScaleEnd  缩放结束时调用。

(2) 使用ScaleGestureDetector

可使用OnScaleGestureListenerSimpleOnScaleGestureListener作为参数;参数使用OnScaleGestureListener需要实现全部方法;参数使用SimpleOnScaleGestureListener只需要实现任意数量的方法。

//参数使用OnScaleGestureListener需要实现全部方法
ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector( context , new ScaleGestureDetector.OnScaleGestureListener() {
    public boolean onScale( ScaleGestureDetector scaleGestureDetector) {
        //缩放时调用。
        return false;
    }
    public boolean onScaleBegin( ScaleGestureDetector scaleGestureDetector) {
        //缩放开始时调用。
        return false;
    }
    public void onScaleEnd( ScaleGestureDetector scaleGestureDetector) {
        //缩放结束时调用。
    }
});


imageView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //ix= motionEvent.getX();
        //iy=motionEvent.getY();
        //传入触碰事件
        scaleGestureDetector.onTouchEvent(motionEvent);
        return true;
    }
});
//参数使用SimpleOnScaleGestureListener只需要实现任意数量的方法
ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector( context ,new ScaleGestureDetector.SimpleOnScaleGestureListener(){
    public boolean onScale( ScaleGestureDetector scaleGestureDetector) {
        //缩放时调用。
        return false;
    }            
});


imageView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //ix= motionEvent.getX();
        //iy=motionEvent.getY();
        //传入触碰事件
        scaleGestureDetector.onTouchEvent(motionEvent);
        return true;
    }
});

3. 混合使用

编程时常会遇到同时需要使用缩放手势和普通手势的时候,这时候就需要进行混合使用。

//缩放手势
ScaleGestureDetector scaleGestureDetector=new ScaleGestureDetector(context,new ScaleGestureDetector.SimpleOnScaleGestureListener(){... ...});

//普通手势
GestureDetector gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){... ...});          

imageView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //缩放
        scaleGestureDetector.onTouchEvent(motionEvent);
        //滑动
        gestureDetector.onTouchEvent(motionEvent);
        return true;
    }
});

  • 27
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在下嗷呜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值