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

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

This lesson describes how to track movement in touch events.

A new  is triggered with an event whenever the current touch contact position, pressure, or size changes. As described in , all of these events are recorded in the  parameter of.

Because finger-based touch isn't always the most precise form of interaction, detecting touch events is often based more on movement than on simple contact. To help apps distinguish between movement-based gestures (such as a swipe) and non-movement gestures (such as a single tap), Android includes the notion of "touch slop." Touch slop refers to the distance in pixels a user's touch can wander before the gesture is interpreted as a movement-based gesture. For more discussion of this topic, see .

There are several different ways to track movement in a gesture, depending on the needs of your application. For example:

  • The starting and ending position of a pointer (for example, move an on-screen object from point A to point B).
  • The direction the pointer is traveling in, as determined by the x and y coordinates.
  • History. You can find the size of a gesture's history by calling the  method . You can then obtain the positions, sizes, time, and pressures of each of the historical events by using the motion event's getHistorical<Value> methods. History is useful when rendering a trail of the user's finger, such as for touch drawing. See the  reference for details.
  • The velocity of the pointer as it moves across the touch screen.

Track Velocity


You could have a movement-based gesture that is simply based on the distance and/or direction the pointer traveled. But velocity often is a determining factor in tracking a gesture's characteristics or even deciding whether the gesture occurred. To make velocity calculation easier, Android provides the  class and the  class in the .  helps you track the velocity of touch events. This is useful for gestures in which velocity is part of the criteria for the gesture, such as a fling.

Here is a simple example that illustrates the purpose of the methods in the  API:

public class MainActivity extends Activity {    private static final String DEBUG_TAG = "Velocity";        ...    private VelocityTracker mVelocityTracker = null;    @Override    public boolean onTouchEvent(MotionEvent event) {        int index = event.getActionIndex();        int action = event.getActionMasked();        int pointerId = event.getPointerId(index);        switch(action) {            case MotionEvent.ACTION_DOWN:                if(mVelocityTracker == null) {                    // Retrieve a new VelocityTracker object to watch the velocity of a motion.                    mVelocityTracker = VelocityTracker.obtain();                }                else {                    // Reset the velocity tracker back to its initial state.                    mVelocityTracker.clear();                }                // Add a user's movement to the tracker.                mVelocityTracker.addMovement(event);                break;            case MotionEvent.ACTION_MOVE:                mVelocityTracker.addMovement(event);                // When you want to determine the velocity, call                 // computeCurrentVelocity(). Then call getXVelocity()                 // and getYVelocity() to retrieve the velocity for each pointer ID.                 mVelocityTracker.computeCurrentVelocity(1000);                // Log velocity of pixels per second                // Best practice to use VelocityTrackerCompat where possible.                Log.d("", "X velocity: " +                         VelocityTrackerCompat.getXVelocity(mVelocityTracker,                         pointerId));                Log.d("", "Y velocity: " +                         VelocityTrackerCompat.getYVelocity(mVelocityTracker,                        pointerId));                break;            case MotionEvent.ACTION_UP:            case MotionEvent.ACTION_CANCEL:                // Return a VelocityTracker object back to be re-used by others.                mVelocityTracker.recycle();                break;        }        return true;    }}

Note: Note that you should calculate velocity after an  event, not after . After an, the X and Y velocities will be 0.

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

你可能感兴趣的文章
深度优先搜索(DFS)和广度优先搜索(BFS)
查看>>
Spark Streaming调优
查看>>
Spark优化
查看>>
Spark
查看>>
Spark自定义排序/分区
查看>>
并查集及其应用
查看>>
Flume与HBASE、Kafka集成
查看>>
SparkStreaming 如何保证消费Kafka的数据不丢失不重复
查看>>
Spark Shuffle及其调优
查看>>
数据仓库分层
查看>>
常见数据结构-TrieTree/线段树/TreeSet
查看>>
Hive数据倾斜
查看>>
TopK问题
查看>>
Hive调优
查看>>
HQL排查数据倾斜
查看>>
DAG以及任务调度
查看>>
LeetCode——DFS
查看>>
MapReduce Task数目划分
查看>>
ZooKeeper分布式锁
查看>>
3126 Prime Path
查看>>