现在的智能手机不敢说百分百的都是触摸屏,也应该是百分之九九以上为触摸屏了,触摸屏为我们 *** 作无键盘、无鼠标的手机系统带来了很多的便利。当用户触摸屏幕时会产生很多的触摸事件,down、up、move等等。VIEw类有个VIEw.OntouchListener内部接口,通过重写他的ontouch(VIEw v,MotionEvent event)方法,我们可以处理一些touch事件,如下:
public class MainActivity extends Activity {...// This example shows an Activity,but you would use the same approach if// you were subclassing a VIEw.@OverrIDepublic boolean ontouchEvent(MotionEvent event){ int action = MotionEventCompat.getActionMasked(event); switch(action) { case (MotionEvent.ACTION_DOWN) : Log.d(DEBUG_TAG,"Action was DOWN"); return true; case (MotionEvent.ACTION_MOVE) : Log.d(DEBUG_TAG,"Action was MOVE"); return true; case (MotionEvent.ACTION_UP) : Log.d(DEBUG_TAG,"Action was UP"); return true; case (MotionEvent.ACTION_CANCEL) : Log.d(DEBUG_TAG,"Action was CANCEL"); return true; case (MotionEvent.ACTION_OUTSIDE) : Log.d(DEBUG_TAG,"Movement occurred outsIDe bounds " + "of current screen element"); return true; default : return super.ontouchEvent(event); } }
Ontouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势。AndroID sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势。
public class GestureDetector extends Object java.lang.ObjectandroID.vIEw.GestureDetector
GestureDetector属于androID.vIEw包,androID还提供了androID.gesture包支持更多的手势 *** 作,以后我们会介绍到。官方的介绍中使用了GestureDetectorCompat处理手势识别,为什么使用GestureDetectorCompat替换了GestureDetector呢,官方的是这样解释的:
GestureDetectorCompat实例化有下面两种方法:
GestureDetector类对外提供了两个接口:OnGestureListener,OnDoubleTapListener,还有一个内部类SimpleOnGestureListener;SimpleOnGestureListener类是GestureDetector提供给我们的一个更方便的响应不同手势的类,它实现了上述两个接口,该类是static class,也就是说它实际上是一个外部类,我们可以在外部继承这个类,重写里面的手势处理方法。因此实现手势识别有两种方法,一种实现OnGestureListener接口,另一种是使用SimpleOnGestureListener类。
OnGestureListener有下面的几个动作:
按下(onDown): 刚刚手指接触到触摸屏的那一刹那,就是触的那一下。
抛掷(onFling): 手指在触摸屏上迅速移动,并松开的动作。
长按(onLongPress): 手指按在持续一段时间,并且没有松开。
滚动(onScroll): 手指在触摸屏上滑动。
按住(onShowPress): 手指按在触摸屏上,它的时间范围在按下起效,在长按之前。
抬起(onSingleTapUp):手指离开触摸屏的那一刹那。
使用OnGestureListener接口,这样需要重载OnGestureListener接口所有的方法,适合监听所有的手势,正如官方文档提到的“Detecing All Supported Gestures”。
public class MainActivity extends Activity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{ private static final String DEBUG_TAG = "Gestures"; private GestureDetectorCompat mDetector; // Called when the activity is first created. @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // Instantiate the gesture detector with the // application context and an implementation of // GestureDetector.OnGestureListener mDetector = new GestureDetectorCompat(this,this); // Set the gesture detector as the double tap // Listener. mDetector.setonDoubleTapListener(this); } @OverrIDe public boolean ontouchEvent(MotionEvent event){ this.mDetector.ontouchEvent(event); // Be sure to call the superclass implementation return super.ontouchEvent(event); } @OverrIDe public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true; } @OverrIDe public boolean onFling(MotionEvent event1,MotionEvent event2,float veLocityX,float veLocityY) { Log.d(DEBUG_TAG,"onFling: " + event1.toString()+event2.toString()); return true; } @OverrIDe public voID onLongPress(MotionEvent event) { Log.d(DEBUG_TAG,"onLongPress: " + event.toString()); } @OverrIDe public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) { Log.d(DEBUG_TAG,"onScroll: " + e1.toString()+e2.toString()); return true; } @OverrIDe public voID onShowPress(MotionEvent event) { Log.d(DEBUG_TAG,"onShowPress: " + event.toString()); } @OverrIDe public boolean onSingleTapUp(MotionEvent event) { Log.d(DEBUG_TAG,"onSingleTapUp: " + event.toString()); return true; } @OverrIDe public boolean onDoubleTap(MotionEvent event) { Log.d(DEBUG_TAG,"onDoubleTap: " + event.toString()); return true; } @OverrIDe public boolean onDoubleTapEvent(MotionEvent event) { Log.d(DEBUG_TAG,"onDoubleTapEvent: " + event.toString()); return true; } @OverrIDe public boolean onSingleTapConfirmed(MotionEvent event) { Log.d(DEBUG_TAG,"onSingleTapConfirmed: " + event.toString()); return true; }}
这样会造成有些手势动作我们用不到,但是还要重载。SimpleOnGestureListener类的出现为我们解决了这个问题,如果你想“Detecting a Subset of Supported Gestures”,SimpleOnGestureListener是最好的选择。
public class MainActivity extends Activity { private GestureDetectorCompat mDetector; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mDetector = new GestureDetectorCompat(this,new MyGestureListener()); } @OverrIDe public boolean ontouchEvent(MotionEvent event){ this.mDetector.ontouchEvent(event); return super.ontouchEvent(event); } class MyGestureListener extends GestureDetector.SimpleOnGestureListener { private static final String DEBUG_TAG = "Gestures"; @OverrIDe public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG,"onDown: " + event.toString()); return true; } @OverrIDe public boolean onFling(MotionEvent event1,float veLocityY) { Log.d(DEBUG_TAG,"onFling: " + event1.toString()+event2.toString()); return true; } }}
最后了我们也解释两个问题:
1、ontouchEvent中为什么使用了MotionEventCompat,而不直接使用MotionEvent。因为MotionEventCompat使更多的Action适配到API 4。
2、AndroID的vIEw怎么使用手势,方法如下:
VIEw myVIEw = findVIEwByID(R.ID.my_vIEw); myVIEw.setontouchListener(new OntouchListener() { public boolean ontouch(VIEw v,MotionEvent event) { // ... Respond to touch events this.mDetector.ontouchEvent(event); return super.ontouchEvent(event); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android触摸及手势 *** 作GestureDetector全部内容,希望文章能够帮你解决Android触摸及手势 *** 作GestureDetector所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)