android 使用手势

在android中,与手势(Gesture)有关的类是GestureDetector。其实GestureDetector这个类也只是把android中基本的触摸事件(ACTION_DOWN、ACTION_MOVE和ACTION_UP)封装了下,提供给我们的是onFling、onLongPress、onScroll等接口。

一、基本使用

1、新建一个继承自SimpleOnGestureListener的类MyGesture,重写onFling事件;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class MyGesture extends SimpleOnGestureListener{
 
// 参数解释:
 
// e1:第1个ACTION_DOWN MotionEvent
 
// e2:最后一个ACTION_MOVE MotionEvent
 
// velocityX:X轴上的移动速度,像素/秒
 
// velocityY:Y轴上的移动速度,像素/秒
 
// 触发条件 : X轴的坐标位移大于FLING_MIN_DISTANCE,
//且移动速度大于FLING_MIN_VELOCITY个像素/秒
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                             float velocityY){
     final int FLING_MIN_DISTANCE= 100 ; //X或者y轴上移动的距离(像素)
     final int FLING_MIN_VELOCITY= 200 ; //x或者y轴上的移动速度(像素/秒)
 
     if ((e1.getX()-e2.getX())>FLING_MIN_DISTANCE
                  && Math.abs(velocityX)>FLING_MIN_VELOCITY)
        Toast.makeText(Main. this , "向左滑动" , Toast.LENGTH_SHORT).show();
     else if ((e2.getX()-e1.getX())>FLING_MIN_DISTANCE
                  && Math.abs(velocityX)>FLING_MIN_VELOCITY)
        Toast.makeText(Main. this , "向右滑动" , Toast.LENGTH_SHORT).show();
 
     return false ;
   }
}

2、实例化一个手势识别对象 GestureDetector gd=new GestrueDetector(new DefaultGestureDetector());

1
2
3
4
5
6
7
8
9
private GestureDetector gesture;
 
@Override
public void onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
 
     setContentView(R.layout.main);
     gesture= new GestureDetector( new MyGesture ());
  }

3、重写控件的onTouchEvent()事件(Activity直接Override,控件就setOnTouchListener());

1
2
3
4
@Override
public boolean onTouchEvent(MotionEvent event){
     return gesture.onTouchEvent(event);
}

二、应用场景

手势其实应用得很多,像左右滑动、电子书阅读器的翻页效果、手势解锁(比如QQ通讯录的隐私琐功能)、双指缩放图片等等。

不管是应用在那种场景,都会遇到一些问题。比如手势与控件其它触摸事件(比如scroll)冲突、手势识别的准确性带来的体验等等。

三、遇到的问题

1、手势与ScrollView控件冲突的解决:主要通过自定义一个ScrollView,并重写其onTouchEvent和dispatchTouchEvent方法来解决冲突问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class MyScrollView extends ScrollView {
 
@Override
public boolean onTouchEvent(MotionEvent ev) {
     super .onTouchEvent(ev);
     return gestureDetector.onTouchEvent(ev);
}
 
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
     gestureDetector.onTouchEvent(ev);
     super .dispatchTouchEvent(ev);
     return true ;
}
 
public MyScrollView(Context context) {
     super (context);
}
 
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
     super (context, attrs, defStyle);
}
 
public MyScrollView(Context context, AttributeSet attrs) {
    super (context, attrs);
}
 
GestureDetector gestureDetector;
 
public void setGestureDetector(GestureDetector gestureDetector) {
     this .gestureDetector = gestureDetector;
}
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值