手势识别官方教程(3)识别移动手势(识别速度用VelocityTracker)

moving手势在onTouchEvent()或onTouch()中就可识别,编程时主要是识别积云的速度用VelocityTracker等,

Tracking Movement

  This lesson describes how to track movement in touch events.

  A new onTouchEvent() is triggered with an ACTION_MOVE event whenever the current touch contact position, pressure, or size changes. As described in Detecting Common Gestures, all of these events are recorded in the MotionEvent parameter of onTouchEvent().

  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 Managing Touch Events in a ViewGroup.

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

Android为了区分swipe(按住一点挑动)和tap(轻按一点)定义了“touch slop”这个新概念,它是在手势被识别成移动手势之前像素的距离。
常见的识别移动手势的方法如下:
  • 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.
    在x或y轴上是否在移动
  • History. You can find the size of a gesture's history by calling the MotionEvent method getHistorySize(). 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 MotionEvent 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 VelocityTracker class and the VelocityTrackerCompat class in the Support LibraryVelocityTracker 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.

识别速度主要用到 VelocityTrackerVelocityTrackerCompat ,下面是示例:

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

 1 public class MainActivity extends Activity {
 2     private static final String DEBUG_TAG = "Velocity";
 3         ...
 4     private VelocityTracker mVelocityTracker = null;
 5     @Override
 6     public boolean onTouchEvent(MotionEvent event) {
 7         int index = event.getActionIndex();
 8         int action = event.getActionMasked();
 9         int pointerId = event.getPointerId(index);
10 
11         switch(action) {
12             case MotionEvent.ACTION_DOWN:
13                 if(mVelocityTracker == null) {
14                     // Retrieve a new VelocityTracker object to watch the velocity of a motion.
15                     mVelocityTracker = VelocityTracker.obtain();
16                 }
17                 else {
18                     // Reset the velocity tracker back to its initial state.
19                     mVelocityTracker.clear();
20                 }
21                 // Add a user's movement to the tracker.
22                 mVelocityTracker.addMovement(event);
23                 break;
24             case MotionEvent.ACTION_MOVE:
25                 mVelocityTracker.addMovement(event);
26                 // When you want to determine the velocity, call 
27                 // computeCurrentVelocity(). Then call getXVelocity() 
28                 // and getYVelocity() to retrieve the velocity for each pointer ID. 
29                 mVelocityTracker.computeCurrentVelocity(1000);
30                 // Log velocity of pixels per second
31                 // Best practice to use VelocityTrackerCompat where possible.
32                 Log.d("", "X velocity: " + 
33                         VelocityTrackerCompat.getXVelocity(mVelocityTracker, 
34                         pointerId));
35                 Log.d("", "Y velocity: " + 
36                         VelocityTrackerCompat.getYVelocity(mVelocityTracker,
37                         pointerId));
38                 break;
39             case MotionEvent.ACTION_UP:
40             case MotionEvent.ACTION_CANCEL:
41                 // Return a VelocityTracker object back to be re-used by others.
42                 mVelocityTracker.recycle();
43                 break;
44         }
45         return true;
46     }
47 }

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

 

 

转载于:https://www.cnblogs.com/sjjg/p/4858995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值