源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android手势操作简单实例讲解

  • 时间:2020-09-17 15:12 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android手势操作简单实例讲解
上一篇介绍的onTouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势。幸好Android SDK给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。 GestureDetector这个类对外提供了两个接口和一个外部类: [b] •接口:[/b]OnGestureListener,OnDoubleTapListener [b] •外部类:[/b]SimpleOnGestureListener 这个外部类,其实是两个接口中所有函数的集成,它包含了这两个接口里所有必须要实现的函数而且都已经重写,但所有方法体都是空的;不同点在于:该类是static class,程序员可以在外部继承这个类,重写里面的手势处理方法。  OnGestureListener有下面的几个动作: [b] •onDown(MotionEvent e):[/b]用户按下屏幕就会触发。 [b] •onShowPress(MotionEvent e):[/b]如果按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPress就会执行,具体这个瞬间是多久,我也不清楚…  [b]•onLongPress(MotionEvent e):[/b]长按触摸屏,超过一定时长,就会触发这个事件。 触发顺序:onDown->onShowPress->onLongPress [b] •onSingleTapUp(MotionEvent e):[/b]一次单独的轻击抬起操作,也就是轻击一下屏幕,立刻抬起来,才会有这个触发,当然,如果除了Down以外还有其它操作,那就不再是Single操作了,所以也就不会触发这个事件。 触发顺序: 点击一下非常快的(不滑动)Touchup: onDown->onSingleTapUp->onSingleTapConfirmed  点击一下稍微慢点的(不滑动)Touchup: onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed  •onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发。 参数解释: e1:第1个ACTION_DOWN MotionEvent e2:最后一个ACTION_MOVE MotionEvent velocityX:X轴上的移动速度,像素/秒 velocityY:Y轴上的移动速度,像素/秒  •onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发。  OnDoubleTapListener有下面的几个动作:  •onSingleTapConfirmed(MotionEvent e):单击事件。用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。 触发顺序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed  •onDoubleTap(MotionEvent e):双击事件。  •onDoubleTapEvent(MotionEvent e):双击间隔中发生的动作。指触发onDoubleTap以后,在双击之间发生的其它动作,包含down、up和move事件。  关于onSingleTapConfirmed和onSingleTapUp的区别:onSingleTapUp,只要手抬起就会执行,而对于onSingleTapConfirmed来说,如果双击的话,则onSingleTapConfirmed不会执行。 [b]使用Gesture[/b]  •使用GestureDetector.OnGestureListener接口  要使用OnGestureListener接口,大致有几步要走: [b]1、创建OnGestureListener监听函数:[/b] private class gestureListener implements GestureDetector.OnGestureListener{  } [b]2、创建GestureDetector实例: [/b]构造函数有下面三个,根据需要选择: GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);  GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);  GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);   注:GestureDetector现在已经是deprecation状态,现在推荐GestureDetectorCompat GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener); GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler); [b] 3、onTouch(View v, MotionEvent event)中拦截[/b],在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector来分析是否有合适的callback函数来处理用户的手势
public boolean onTouch(View v, MotionEvent event) { 
 return gestureDetectorCompat.onTouchEvent(event);  
} 

[b] 4、控件绑定[/b] TextView tv = (TextView)findViewById(R.id.tv); tv.setOnTouchListener(this);  实现代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"> 

 <TextView 
  android:id="@+id/tv" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:layout_margin="50dip" 
  android:background="#76EE00" 
  android:text="Gesture Detector" /> 
</RelativeLayout>

public class MainActivity extends Activity implements OnTouchListener{  
 private GestureDetectorCompat mGestureDetectorCompat; 

 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main);   
  mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener());   
  TextView tv = (TextView)findViewById(R.id.tv); 
  tv.setOnTouchListener(this); 
  tv.setFocusable(true);  
  tv.setClickable(true);  
  tv.setLongClickable(true); 
 } 

 public boolean onTouch(View v, MotionEvent event) { 
  return mGestureDetectorCompat.onTouchEvent(event);  
 } 

 private class gestureListener implements GestureDetector.OnGestureListener{ 
  public boolean onDown(MotionEvent e) { 
   showlog("onDown");  
   Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();  
   return false; 
  } 

  public void onShowPress(MotionEvent e) {
   showlog("onShowPress");  
   Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();  
  } 

  public boolean onSingleTapUp(MotionEvent e) { 
   showlog("onSingleTapUp");  
   Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();  
   return true;  
  } 

  public boolean onScroll(MotionEvent e1, MotionEvent e2, 
    float distanceX, float distanceY) { 
   showlog("onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX);  
   Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();   
   return true;  
  } 

  public void onLongPress(MotionEvent e) { 
    showlog("onLongPress");  
    Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();  
  } 

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
    float velocityY) { 
   showlog("onFling");  
   Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();  
   return true; 
  } 
 }; 

 public void showlog(String info) {
  System.out.print("GestureDetector "+info);
 } 
} 

 •使用GestureDetector.OnDoubleTapListener接口  实现OnDoubleTapListener接口的前提是先实现OnGestureListener接口,其实除了第1步,2、3、4步和上面完全一样,不再赘述,下面看下第一步: 同时创建OnGestureListener和OnDoubleTapListener监听函数: [b]方法一:[/b]新建一个类同时派生自OnGestureListener和OnDoubleTapListener:
[url=http://xiazai.jb51.net/201609/yuanma/GestureDetector(jb51.net).rar]http://xiazai.jb51.net/201609/yuanma/GestureDetector(jb51.net).rar[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部