編輯:關於Android編程
這個課程描述了如何在觸摸事件中跟蹤移動。
無論當前觸摸接觸點的位置,壓力,或者大小的變化,onTouchEvenet()方法被一個ACTION_MOVE事件觸發。正如在Detecting Common Gestures中描述,所有的這些事件都被記錄在onTouchEvent()方法的MotionEvent參數中。
因為基於手指的觸摸不總是最精確的交互形式,檢測觸摸時間經常是基於移動而不是簡單的觸摸。為了幫助應用程序區分在基於移動的手勢(例如swip)和沒有移動的手勢(例如一個單獨的輕敲),Android包含”觸摸溢出“的概念。觸摸溢出在手勢被翻譯為一個基於移動的手勢之前,引用了用戶的觸摸移動的像素距離。更多這個話題的討論,查閱Managing Touch Evenet in a ViewGroup。
這裡有多種不同的方式來在一個手勢中跟蹤移動,基於你的應用程序的需要。例如:
一個點的開始和結束的位置(例如,移動一個屏幕對象,從點A到點B)。
點移動的方向,通過x和y坐標來被決定。
歷史。通過調用MotionEvent方法getHistorySize()方法,你能找到這個手勢的歷史的大小。你然後通過這個運動事件的getHistorical
這個點它在屏幕上移動的速率。
—————————————————————————————————————————————————————————————————
你能擁有一個基於移動的手勢,它僅僅基於點移動的距離和/或者方向。但是速率經常是跟蹤一個手勢的特征,或者甚至是決定一個手勢是否發生的決定因素。為了使速率計算簡單,Android在支持庫中提供了VelocityTracker類和VelocityTrackerCompat類。VelocityTracker幫助你跟蹤這個觸摸事件的速率。這對於手勢是非常有用的,在速率是這個手勢的准則的時候,例如一個fling。
下面是一個簡單示例,它說明了在VelocityTracker 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;
}
} 注意:
注意你應該在一個ACTION_MOVE事件之後計算速率,不要在ACTION_UP。在ACTION_UP之後,X和Y速度將為0。
Android 知識要點整理(12)----Animation(動畫)
動畫分類Android動畫有3類:幀動畫、視圖動畫、屬性動畫。幀動畫和視圖動畫又統稱為補間動畫。Android 3.0(API LEVEL 11)開始支持屬性動畫。幀動畫
Android開發之android_apk 在線安裝(源代碼分享)
android_apk的在線安裝,除了要設計Android 客戶端的代碼外,還要搭建服務器的代碼,仿真實現中Android軟件的在線升級。 Android 客
Android數據庫之創建和升級數據庫(中)
上一篇文章中,簡單介紹了一下android數據庫的一些基本概念,那麼從本節開始,就實戰一下Android數據庫的創建和升級。 上文中,也介紹了,SQLiteOpenHel
Android ViewPager實現無限循環效果
最近項目裡有用到ViewPager來做廣告運營位展示,看到現在很多APP的廣告運營位都是無限循環的,所以就研究了一下這個功能的實現。先看看效果從一個方向上一直滑動,麼有滑
模板特化和偏模板特化例子(template specialization and partial template specialization)
測試環境: win7 64 g++ 4.8.1 /*