博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using Touch Gestures 》Detecting Common Gestures
阅读量:4207 次
发布时间:2019-05-26

本文共 9405 字,大约阅读时间需要 31 分钟。

A "touch gesture" occurs when a user places one or more fingers on the touch screen, and your application interprets that pattern of touches as a particular gesture. There are correspondingly two phases to gesture detection:

  1. Gathering data about touch events.
  2. Interpreting the data to see if it meets the criteria for any of the gestures your app supports.

Support Library Classes

The examples in this lesson use the and  classes. These classes are in the. You should use Support Library classes where possible to provide compatibility with devices running Android 1.6 and higher. Note that  is not a replacement for the  class. Rather, it provides static utility methods to which you pass your object in order to receive the desired action associated with that event.

Gather Data


When a user places one or more fingers on the screen, this triggers the callback  on the View that received the touch events. For each sequence of touch events (position, pressure, size, addition of another finger, etc.) that is ultimately identified as a gesture,  is fired several times.

The gesture starts when the user first touches the screen, continues as the system tracks the position of the user's finger(s), and ends by capturing the final event of the user's fingers leaving the screen. Throughout this interaction, the  delivered to  provides the details of every interaction. Your app can use the data provided by the  to determine if a gesture it cares about happened.

Capturing touch events for an Activity or View

To intercept touch events in an Activity or View, override the  callback.

The following snippet uses  to extract the action the user performed from the eventparameter. This gives you the raw data you need to determine if a gesture you care about occurred:

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);    }      }

You can then do your own processing on these events to determine if a gesture occurred. This is the kind of processing you would have to do for a custom gesture. However, if your app uses common gestures such as double tap, long press, fling, and so on, you can take advantage of the  class. makes it easy for you to detect common gestures without processing the individual touch events yourself. This is discussed below in .

Capturing touch events for a single view

As an alternative to , you can attach an  object to any  object using the  method. This makes it possible to to listen for touch events without subclassing an existing . For example:

View myView = findViewById(R.id.my_view); myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // ... Respond to touch events               return true;    }});

Beware of creating a listener that returns false for the  event. If you do this, the listener will not be called for the subsequent  and  string of events. This is because is the starting point for all touch events.

If you are creating a custom View, you can override , as described above.

Detect Gestures


Android provides the  class for detecting common gestures. Some of the gestures it supports include , and so on. You can use  in conjunction with the method described above.

Detecting All Supported Gestures

When you instantiate a  object, one of the parameters it takes is a class that implements the  interface.  notifies users when a particular touch event has occurred. To make it possible for your  object to receive events, you override the View or Activity's  method, and pass along all observed events to the detector instance.

In the following snippet, a return value of true from the individual on<TouchEvent> methods indicates that you have handled the touch event. A return value of false passes events down through the view stack until the touch has been successfully handled.

Run the following snippet to get a feel for how actions are triggered when you interact with the touch screen, and what the contents of the  are for each touch event. You will realize how much data is being generated for even simple interactions.

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;    }}

Detecting a Subset of Supported Gestures

If you only want to process a few gestures, you can extend  instead of implementing the  interface.

 provides an implementation for all of the on<TouchEvent>methods by returning false for all of them. Thus you can override only the methods you care about. For example, the snippet below creates a class that extends  and overrides and .

Whether or not you use , it's best practice to implement an method that returns true. This is because all gestures begin with an  message. If you return falsefrom , as  does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of  never get called. This has the potential to cause unexpected problems in your app. The only time you should return falsefrom  is if you truly want to ignore an entire gesture.

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, MotionEvent event2,                 float velocityX, float velocityY) {
            Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());            return true;        }    }}

转载地址:http://xxlli.baihongyu.com/

你可能感兴趣的文章
WF从入门到精通(第十一章):并行活动
查看>>
WF从入门到精通(第十二章):策略和规则
查看>>
WF从入门到精通(第十三章):打造自定义活动
查看>>
WF从入门到精通(第十四章):基于状态的工作流
查看>>
WF从入门到精通(第十五章):工作流和事务
查看>>
WF从入门到精通(第十六章):声明式工作流
查看>>
WF从入门到精通(第十七章):关联及本地主机通信
查看>>
WF从入门到精通(第十八章):在你的工作流中调用Web服务
查看>>
WF从入门到精通(第十九章):把工作流发布为Web服务
查看>>
WF综合技术演示的例子
查看>>
WF编程学习 基本活动:状态活动
查看>>
你戴错眼镜啦
查看>>
近视眼从800度降到现在的100度。我今年25岁(转)
查看>>
我把近视从600度直降到0度 作者:灵魂旗舰 谈谈治疗近视的雾视疗法
查看>>
近视眼如何自然恢复
查看>>
世界十大风景名胜[图]
查看>>
2008世界GDP排名
查看>>
C#静态类
查看>>
C#逻辑运算符简介
查看>>
职场中人自我认定的天涯精华帖(链接地址、读后感、经典语录)看完就更新
查看>>