当前位置: 移动技术网 > IT编程>移动开发>Android > Android 手势操作编程详解

Android 手势操作编程详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

铃声排行,周大生黄金,裤带蛇

      手势操作在我们使用智能设备的过程中奉献了不一样的体验。android开发中必然会进行手势操作方面的编程。那么它的原理是怎样的呢?我们如何进行手势操作编程呢?

       手势操作原理

       首先,在android系统中,每一次手势交互都会依照以下顺序执行。

       1. 接触接触屏一刹那,触发一个motionevent事件。

       2. 该事件被ontouchlistener监听,在其ontouch()方法里获得该motionevent对象。

       3. 通过gesturedetector(手势识别器)转发次motionevent对象至ongesturelistener。

       4. ongesturelistener获得该对象,听根据该对象封装的的信息,做出合适的反馈。

       这个顺序可以说就是手势操作的原理。

       手势操作类和接口

       下面一同来了解一下motionevent、gesturedetector和ongesturelistener

       motionevent: 这个类用于封装手势、触摸笔、轨迹球等等的动作事件。其内部封装了两个重要的属性x和y,这两个属性分别用于记录横轴和纵轴的坐标。

       gesturedetector: 识别各种手势。

       ongesturelistener: 这是一个手势交互的监听接口,其中提供了多个抽象方法,并根据gesturedetector的手势识别结果调用相对应的方法。

       手势操作实例

       下面我再通过一个切换美女图片的代码示例,演示一下手势交互的实现,让大伙对上面的执行顺序,以及各手势动作的区分有一个更加深刻的了解和记忆。

       首先,提供一个只有imageview的布局文件——main.xml。

xml/html代码

<?xml version="1.0" encoding="utf-8"?>  
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
   <imageview android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"/>  
</linearlayout>  

       然后,完成我们的activity,因为要监听触摸屏的触摸事件和手势时间,所以该activity必须实现ontouchlistener和ongesturelistener两个接口,并重写其中的方法。具体代码如下:

java代码

public class mainactivity extends activity implements ontouchlistener, ongesturelistener {  
    
   //创建一个用于识别收拾的gesturedetector对象waiyuwu.blogcn.com  
   private gesturedetector detector = new gesturedetector(this);  
   //定义一个数组,用于放漂亮的女孩  
   int[] girls = new int[]{r.drawable.girl1, r.drawable.girl2, r.drawable.girl3};  
   //定义数组下标,以方便观看各个女孩  
   private int index;  
   private imageview image;  
    
   @override  
   public void oncreate(bundle savedinstancestate) {  
    super.oncreate(savedinstancestate);  
    setcontentview(r.layout.main);  
      
    image = (imageview)findviewbyid(r.id.image);  
    //设置一个初始显示的girl吧  
    image.setimageresource(girls[index]);  
    //监听这个imageview组件上的触摸屏时间  
    image.setontouchlistener(this);  
    //下面两个要记得设哦,不然就没法处理轻触以外的事件了,例如抛掷动作。  
    image.setlongclickable(true);  
    detector.setislongpressenabled(true);  
   }  
    
   //用于呼喊下一个女孩的方法  
   public void gonext(){  
     index++;  
     index = math.abs(index % girls.length);  
     image.setimageresource(girls[index]);  
   }  
    
   //用户呼唤上一个女孩的方法  
   public void goprevious(){  
     index--;  
     index = math.abs(index % girls.length);  
     image.setimageresource(girls[index]);  
   }  
  
   //重写ontouchlistener的ontouch方法  
   //此方法在触摸屏被触摸,即发生触摸事件(接触和抚摸两个事件,挺形象)的时候被调用。  
   @override  
   public boolean ontouch(view v, motionevent event) {  
     detector.ontouchevent(event);  
     return true;  
   }  
  
   //在按下动作时被调用  
   @override  
   public boolean ondown(motionevent e) {  
     return false;  
   }  
  
   //在抛掷动作时被调用  
   @override  
   public boolean onfling(motionevent e1, motionevent e2, float velocityx,  
       float velocityy) {  
     //velocityx表示横向的移动,根据手指移动的方向切换女孩  
     if(velocityx < 0){  
       gonext();  
     }else if(velocityx > 0){  
       goprevious();  
     }  
     return false;  
   }  
  
   //在长按时被调用  
   @override  
   public void onlongpress(motionevent e) {  
   }  
  
   //在滚动时调用  
   @override  
   public boolean onscroll(motionevent e1, motionevent e2, float distancex,  
       float distancey) {  
     return false;  
   }  
  
   //在按住时被调用  
   @override  
   public void onshowpress(motionevent e) {  
   }  
  
   //在抬起时被调用  
   @override  
   public boolean onsingletapup(motionevent e) {  
     return false;  
   }  
 }  

       手势操作各个方法的含义

       在刚开始学android的时候,就觉得google的文档不咋样,在研究手势时,更加的感觉google的文档写得实在是太差了。很多常量, 属性和方法,居然连个描述都没有。没有描述也就罢了,但是ongesturelistener里手势这么多,它也没有一个介绍说明,在没有进行不断才尝试 之前,谁能搞懂onlongpress和onshowpress,onscroll和onfling的关系与差别吗?google真的需要在文档方面做一次大手术了。不过好在经过鄙人不断反复的尝试。从个人的角度为这几个手势动作做出了定义。

  1.        按下(ondown): 刚刚手指接触到触摸屏的那一刹那,就是触的那一下。
  2.        抛掷(onfling): 手指在触摸屏上迅速移动,并松开的动作。
  3.        长按(onlongpress): 手指按在持续一段时间,并且没有松开。
  4.        滚动(onscroll): 手指在触摸屏上滑动。
  5.        按住(onshowpress): 手指按在触摸屏上,它的时间范围在按下起效,在长按之前。
  6.        抬起(onsingletapup):手指离开触摸屏的那一刹那。

       除了这些定义之外,鄙人也总结了一点算是经验的经验吧,在这里和大家分享一下。

       任何手势动作都会先执行一次按下(ondown)动作。

  1.         长按(onlongpress)动作前一定会执行一次按住(onshowpress)动作。
  2.        按住(onshowpress)动作和按下(ondown)动作之后都会执行一次抬起(onsingletapup)动作。
  3.        长按(onlongpress)、滚动(onscroll)和抛掷(onfling)动作之后都不会执行抬起(onsingletapup)动作。

          以上就是对android 手势操作的资料整理,后续继续补充相关知识,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网