編輯:初階開發
在遊戲開發中,我們經常需要用到雙擊螢幕的交互。在 Android 1.6 以前,系統尚未提供完善的手勢識別類。雖然在 Android 1.5 SDK 中能找到 android.view.GestureDetector.OnDoubleTapListener,但經測試卻無法正常工作。最終,我們採用了下面的解決方案。
public class TouchLayout extends RelativeLayout {
public Handler doubleTapHandler = null;
protected long lastDown = -1;
public final static long DOUBLE_TIME = 500;
public TouchLayout(Context context) {
super(context);
}
public TouchLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean onTouchEvent(MotionEvent event) {
this.handleEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
long nowDown = System.currentTimeMillis();
if (nowDown - lastDown < DOUBLE_TIME)
{
if (doubleTapHandler != null)
doubleTapHandler.sendEmptyMessage(-1);
} else {
lastDown = nowDown;
}
}
return true;
}
protected void handleEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do sth 這裡處理即可
break;
case MotionEvent.ACTION_UP:
//Do sth
break;
}
}
}
這個方案的核心思路很直觀:記錄上一次按下螢幕的時間戳 lastDown,每次 ACTION_DOWN 時判斷與上次的時間間隔是否小於閾值 DOUBLE_TIME(此處設為 500 毫秒)。若在閾值以內,即認定為雙擊,透過 Handler 發送消息通知相應的處理邏輯。這種基於時間差判斷的實現,能有效繞開早期系統手勢類的兼容問題,為遊戲提供穩定的雙擊交互。
Android 2.3.3 抵達 Nexus One:新增 WebM 影片支援
Google 已開始向兩款旗艦機 Nexus One 與 Nexus S 推送 Android 2.3.3。這是 Gingerbread 官方版發表近兩個月後,Nexus
AppWidgetManager 類別重點整理
AppWidgetManager 是 Android 平台上最主要的小工具(Widget)管理類,負責更新 AppWidget 的狀態、取得已安裝 Widget 的資訊,
用 PackageManager 取得所有啟動器應用清單
要列出裝置上所有出現在桌面(Launcher)裡的應用,關鍵是 PackageManager 搭配一個特定的 Intent 組合。以下把實作拆成幾個步驟。 1. 定義一
OPhone:中國移動以 Android 為軸的生態布局
圖為 OPhone 的水平分工體系:通信運營商中國移動居於主導地位,軟體開發與終端製造則由播思通訊與各終端廠商負責,日本的窗口為鵬開科技。 分工體系與市場版圖 以 A