Android官方开发文档Training系列课程中文版:手势处理之记录手指移动的轨迹
原文地址:http://android.xsoftlab.net/training/gestures/movement.html
這節(jié)課將會學習如何在觸摸事件中記錄手指移動的軌跡。
當手指觸摸的位置、壓力或者尺寸發(fā)生變化時,ACTION_MOVE事件就會被觸發(fā)。與Detecting Common Gestures中描述的一樣,所有的事件都被記錄在一個MotionEvent對象中。
因為基于手指的觸摸并不是很精確的交互方式,所以檢測觸摸事件的行為需要更多的軌跡點。為了幫助APP區(qū)分基于軌跡的手勢(比如滑動等移動的手勢)與非軌跡手勢(比如單點等不移動的手勢),Android提出了一個名為”touch slop”的概念。Touch slop指的是用戶按下的以像素為單位的距離。
這里有若干項不同的追蹤手勢軌跡的方法,具體使用哪個方法取決于應用程序的需求:
- 指針的起始位置與結束位置。
- 指針位移的方向,由X,Y的坐標判斷。
- 歷史記錄,你可以通過getHistorySize()獲得手勢的歷史尺寸。然后可以通過getHistorical(Value)方法獲得這些歷史事件的位置,尺寸,事件以及壓力。當渲染手指的軌跡時,比如在屏幕上用手指畫線條等,歷史記錄這時就會派上用場。
- 指針在屏幕上滑動的速度。
軌跡的速度
在記錄手勢的特性或者在檢查何種手勢事件發(fā)生時,除了要依靠手指移動的距離、方向這兩個要素之外。還需要另外一個非常重要的因素就是速度。為了使速度計算更加容易,Android為此提供了VelocityTracker類以及VelocityTrackerCompat類。VelocityTracker用于輔助記錄觸摸事件的速度。這對于判斷哪個速度是手勢的標準部分,比如飛速滑動。
下面的例子用于演示在VelocityTracker API中方法的目的:
public class MainActivity extends Activity {private static final String DEBUG_TAG = "Velocity";...private VelocityTracker mVelocityTracker = null;@Overridepublic 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: 注意,應當在ACTION_MOVE事件內(nèi)部計算速度,不要在ACTION_UP內(nèi)部計算,因為在ACTION_UP內(nèi)部計算所得到的X與Y的速度值都是0.
總結
以上是生活随笔為你收集整理的Android官方开发文档Training系列课程中文版:手势处理之记录手指移动的轨迹的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android官方开发文档Trainin
- 下一篇: leetcode--数组(Medium2