android学习--------Gestures(手势)

来源:互联网 发布:遗传算法原理与应用 编辑:程序博客网 时间:2024/06/06 01:04

先介绍几个基本手势:

❉按下(onDown):刚刚收手指接触到触摸屏的那一刹那,就是触的那一下。

❉抛掷(onFling):手指在触摸屏上迅速移动,并松开的动作。

❉长按(onLongPress):手指按在持续一段时间,并且没有松开。

❉滚动(onScroll):手指在触摸屏上滑动。

❉按住(onShowPress):手指按在触摸屏上,它的时间范围在按下起效,在长按之前。

❉抬起(onSIngleTapUp):手指离开触摸屏的那一刹那。


当然在平时使用时我们用到的组合手势有:

❉按下后立即松开(轻触):

❉长按后松开:

❉轻轻一划,同时松开:

❉按住后不放持续做滑动操作:

那这些组合手势依次执行哪些基本手势?

我们先写个程序测试一下:

只需在主程序写上如下代码:

package com.example.administrator.shock;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.GestureDetector;import android.view.MotionEvent;public class MainActivity extends AppCompatActivity {    private MyGestureListener mgListener;    private GestureDetector mDetector;    private final static String TAG = "MyGesture";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //实例化GestureListener与GestureDetector对象        mgListener = new MyGestureListener();        mDetector = new GestureDetector(this, mgListener);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        return mDetector.onTouchEvent(event);    }    //自定义一个GestureListener,这个是View类下的,别写错哦!!!    private class MyGestureListener implements GestureDetector.OnGestureListener {        @Override        public boolean onDown(MotionEvent motionEvent) {            Log.d(TAG, "onDown:按下");            return false;        }        @Override        public void onShowPress(MotionEvent motionEvent) {            Log.d(TAG, "onShowPress:手指按下一段时间,不过还没到长按");        }        @Override        public boolean onSingleTapUp(MotionEvent motionEvent) {            Log.d(TAG, "onSingleTapUp:手指离开屏幕的一瞬间");            return false;        }        @Override        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {            Log.d(TAG, "onScroll:在触摸屏上滑动");            return false;        }        @Override        public void onLongPress(MotionEvent motionEvent) {            Log.d(TAG, "onLongPress:长按并且没有松开");        }        @Override        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {            Log.d(TAG, "onFling:迅速滑动,并松开");            return false;        }    }}
测试结果如下:

❉按下后立即松开(轻触):

09-16 09:29:11.587 26434-26434/com.example.administrator.shock D/MyGesture: onDown:按下
09-16 09:29:11.617 26434-26434/com.example.administrator.shock D/MyGesture: onSingleTapUp:手指离开屏幕的一瞬间

❉长按后松开:

09-16 09:35:05.187 26434-26434/com.example.administrator.shock D/MyGesture: onDown:按下
09-16 09:35:05.367 26434-26434/com.example.administrator.shock D/MyGesture: onShowPress:手指按下一段时间,不过还没到长按
09-16 09:35:05.867 26434-26434/com.example.administrator.shock D/MyGesture: onLongPress:长按并且没有松开

❉轻轻一划,同时松开:

09-16 09:36:07.897 26434-26434/com.example.administrator.shock D/MyGesture: onDown:按下
09-16 09:36:07.937 26434-26434/com.example.administrator.shock D/MyGesture: onScroll:在触摸屏上滑动
09-16 09:36:07.957 26434-26434/com.example.administrator.shock D/MyGesture: onScroll:在触摸屏上滑动
09-16 09:36:07.977 26434-26434/com.example.administrator.shock D/MyGesture: onScroll:在触摸屏上滑动
09-16 09:36:07.987 26434-26434/com.example.administrator.shock D/MyGesture: onScroll:在触摸屏上滑动
09-16 09:36:07.997 26434-26434/com.example.administrator.shock D/MyGesture: onFling:迅速滑动,并松开

❉按住后不放持续做滑动操作:

09-16 09:38:10.957 26434-26434/com.example.administrator.shock D/MyGesture: onDown:按下
09-16 09:38:11.127 26434-26434/com.example.administrator.shock D/MyGesture: onShowPress:手指按下一段时间,不过还没到长按
09-16 09:38:11.247 26434-26434/com.example.administrator.shock D/MyGesture: onScroll:在触摸屏上滑动
09-16 09:38:11.267 26434-26434/com.example.administrator.shock D/MyGesture: onScroll:在触摸屏上滑动


根据上面结果我们可以发现:
❉任何手势动作都会先执行一次按下(onDown)动作。

❉长按(onLongPress)动作前一定会执行依次(onShowPress)。

❉按住(onShowPress)动作和按下(onDown)动作之后都会执行一次抬起(onSingleTapUp)。

❉长按(onLongPress),滚动(onScroll)和抛掷动作(onFling)动作之后都不会执行抬起(onSingleTapUp)动作。


有了这些我们来看android如何实现手势检测的?

Android提供手势检测,并为手势识别提供了相应的监听器

具体请看官方APIGestures,也可以下载中文API文档,看博客


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


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

2.该事件被OnTouchListener监听,在其onTouch()方法中获得该MotionEvent对象

3.通过GestureDetector转发Motionevent对象给OnGestureListener

4.通过OnGestureListener获取相关信息,以及做相关处理。


官方文档中是这样写的:

结构

继承关系

public class GestureDetector extendsObject

        

java.lang.Object

android.view.GestureDetector

 

类概述

通过系统提供的MotionEvent来监测各种手势和(触摸)事件。当一个指定的手势事件发生时,GestureDetector.OnGestureListener回调函数将通告用户。这个类仅仅处理由触摸引发的MotionEvent(不能处理由轨迹球引发的事件)。要使用这个类需执行以下操作:

l   为你的View建立一个GestureDetector实例。

l   在View的onTouchEvent(MotionEvent)方法里确保调用(GestureDetector的)onTouchEvent(MotionEvent)方法。当相关事件发生时,定义在回调函数里的方法将被执行。

 

例子的话上面已举出。

但从上述结果来看,我们发现了一个问题,我们实现OnGestureListener需要实现所有的手势,可能我们仅仅需要滑动,但却还是需要重载很多种基本手势,对此官方给出了解决方法,官方文档中

interface          GestureDetector.OnGestureListener

在有手势动作发生时,通知的监听器

 

class        GestureDetector.SimpleOnGestureListener         

当只需要监听部分手势时,用于扩展的便捷类


使用如下的例子:实现向左滑动进入下个界面,向右滑动则退出程序。


主程序代码:


package com.example.administrator.shock;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.GestureDetector;import android.view.MotionEvent;public class MainActivity extends AppCompatActivity {    private final static int MIN_MOVE = 200;   //最小距离    private MyGestureListener mgListener;    private GestureDetector mDetector;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //实例化GestureListener与GestureDetector对象        mgListener = new MyGestureListener();        mDetector = new GestureDetector(this, mgListener);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        return mDetector.onTouchEvent(event);    }    //自定义一个GestureListener,这个是View类下的,别写错哦!!!    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {        @Override        public boolean onFling(MotionEvent e1, MotionEvent e2, float v1, float v2) {if(e2.getX()-e1.getX()>=MIN_MOVE)    startActivity(new Intent(MainActivity.this,new_Activity.class));            elsefinish();            return false;        }    }}


效果如下:


0 0
原创粉丝点击