在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 在不同的 Android 系統(tǒng)版本支持控制器
檢測常用的手勢
優(yōu)化layout的層級
用戶輸入
管理應用的內存
聯(lián)系人信息
開發(fā)輔助程序
Android多媒體
添加語音功能
顯示位置地址
提供向下與橫向導航
支持游戲控制器
訪問可穿戴數(shù)據(jù)層
處理多點觸控手勢
全屏沉浸式應用
為多線程創(chuàng)建管理器
數(shù)據(jù)保存
Intent的發(fā)送
更新Notification
優(yōu)化下載以高效地訪問網(wǎng)絡
打印
打包可穿戴應用
接收從其他App傳送來的數(shù)據(jù)
發(fā)送與接收消息
建立靈活動態(tài)的UI
處理鍵盤輸入
Building a Work Policy Controller
建立測試環(huán)境
創(chuàng)建表盤
分享文件
顯示Notification進度
實現(xiàn)自適應UI流(Flows)
使用設備管理策略增強安全性
使用能感知版本的組件
執(zhí)行網(wǎng)絡操作
建立文件分享
添加移動
更新你的Security Provider來對抗SSL漏洞利用
支持鍵盤導航
創(chuàng)建和監(jiān)視地理圍欄
發(fā)送并同步數(shù)據(jù)
使用BigView樣式
無線連接設備
提供向上導航與歷史導航
最小化定期更新造成的影響
實現(xiàn)向下的導航
支持不同的屏幕大小
Android 可穿戴應用
添加動畫
顯示聯(lián)系人頭像
使用OpenGL ES顯示圖像
處理輸入法可見性
分享文件
保持設備喚醒
淡化系統(tǒng)Bar
使用NFC分享文件
保存到Preference
Android聯(lián)系人信息與位置信息
創(chuàng)建標準的網(wǎng)絡請求
使用Drawables
管理Bitmap的內存使用
管理Activity的生命周期
按需加載視圖
傳輸資源
為可穿戴設備創(chuàng)建自定義UI
在一個線程中執(zhí)行一段特定的代碼
性能優(yōu)化
隱藏導航欄
創(chuàng)建目錄瀏覽器
為多種大小的屏幕進行規(guī)劃
View間漸變
使用觸摸手勢
高效加載大圖
使用CursorLoader在后臺加載數(shù)據(jù)
創(chuàng)建抽屜式導航(navigation drawer)
管理音頻焦點
創(chuàng)建后臺服務
創(chuàng)建功能測試
創(chuàng)建使用Material Design的應用
停止與重啟Activity
添加一個簡便的分享功能
啟動Activity時保留導航
TV應用清單
創(chuàng)建向后兼容的UI
?# 優(yōu)化自定義View
創(chuàng)建單元測試
在UI上顯示Bitmap
建立OpenGL ES的環(huán)境
構建表盤服務
JNI Tips
建立搜索界面
實現(xiàn)自定義View的繪制
使用HTTPS與SSL
按需操控BroadcastReceiver
分享簡單的數(shù)據(jù)
繪制形狀
Android位置信息
創(chuàng)建并運行可穿戴應用
執(zhí)行 Sync Adpater
獲取最后可知位置
創(chuàng)建 Android 項目
實現(xiàn)高效的導航
退出全屏的Activity
創(chuàng)建Card
兼容音頻輸出設備
同步數(shù)據(jù)單元
傳輸數(shù)據(jù)時避免消耗大量電量
保存到文件
緩存Bitmap
提供配置 Activity
調度重復的鬧鐘
實現(xiàn)輔助功能
重復的下載是冗余的
隱藏狀態(tài)欄
實現(xiàn)自定義的網(wǎng)絡請求
規(guī)劃界面和他們之間的關系
使用Sync Adapter傳輸數(shù)據(jù)
TV應用內搜索
響應觸摸事件
使用Google Cloud Messaging(已廢棄)
控制相機
Android網(wǎng)絡連接與云服務
請求分享一個文件
處理TV硬件
響應UI可見性的變化
使用網(wǎng)絡服務發(fā)現(xiàn)
指定輸入法類型
優(yōu)化電池壽命
創(chuàng)建TV應用
獲取聯(lián)系人列表
拖拽與縮放
啟動與停止線程池中的線程
創(chuàng)建 Sync Adpater
使用 WiFi P2P 服務發(fā)現(xiàn)
開始使用Material Design
代理至新的APIs
使用include標簽重用layouts
使得View可交互
高效顯示Bitmap
創(chuàng)建企業(yè)級應用
Fragments之間的交互
創(chuàng)建與執(zhí)行測試用例
綜合:設計我們的樣例 App
繪制表盤
建立簡單的用戶界面
自定義動畫
開發(fā)輔助服務
避免出現(xiàn)程序無響應ANR(Keeping Your App Responsive)
使用ViewPager實現(xiàn)屏幕滑動
設計高效的導航
Android分享操作(Building Apps with Content Sharing)
提供向后的導航
保持向下兼容
創(chuàng)建TV播放應用
縮放View
使用 WiFi 建立 P2P 連接
Android后臺任務
連接到網(wǎng)絡
為 Notification 添加頁面
使TV應用是可被搜索的
添加Action Bar
使用Material的主題
啟動另一個Activity
顯示正在播放卡片
適配不同的系統(tǒng)版本
輕松錄制視頻
創(chuàng)建可穿戴的應用
創(chuàng)建自定義的布局
重新創(chuàng)建Activity
使用CursorLoader執(zhí)行查詢任務
使用舊的APIs實現(xiàn)新API的效果
使用備份API
安全要點
Android入門基礎:從這里開始
保存并搜索數(shù)據(jù)
根據(jù)網(wǎng)絡連接類型來調整下載模式
使用Tabs創(chuàng)建Swipe視圖
SMP(Symmetric Multi-Processor) Primer for Android
解析 XML 數(shù)據(jù)
使用 Volley 傳輸網(wǎng)絡數(shù)據(jù)
建立ActionBar
Android交互設計
使用Intent修改聯(lián)系人信息
增加搜索功能
輕松拍攝照片
定義形狀
測試你的Activity
在 Notifcation 中接收語音輸入
與其他應用的交互
管理系統(tǒng)UI
追蹤手勢移動
Android界面設計
執(zhí)行 Android 程序
顯示確認界面
創(chuàng)建Lists與Cards
打印HTML文檔
創(chuàng)建TV應用
為多屏幕設計
定義Shadows與Clipping視圖
使用Fragment建立動態(tài)UI
接收Activity返回的結果
布局變更動畫
定位常見的問題
自定義ActionBar的風格
定義Layouts
發(fā)送簡單的網(wǎng)絡請求
啟動與銷毀Activity
與UI線程通信
非UI線程處理Bitmap
創(chuàng)建TV布局
提升Layout的性能
報告任務執(zhí)行狀態(tài)
判斷并監(jiān)測網(wǎng)絡連接狀態(tài)
兼容不同的設備
處理按鍵動作
優(yōu)化性能和電池使用時間
給其他App發(fā)送簡單的數(shù)據(jù)
Implementing App Restrictions
向后臺服務發(fā)送任務請求
展示Card翻轉動畫
管理ViewGroup中的觸摸事件
兼容不同的屏幕密度
通過藍牙進行調試
為可穿戴設備創(chuàng)建Notification
控制音量與音頻播放
獲取聯(lián)系人詳情
在表盤上顯示信息
提供向上的導航
滾動手勢動畫
幫助用戶在TV上找到內容
創(chuàng)建TV導航
為索引指定App內容
ActionBar的覆蓋疊加
Android Wear 上的位置檢測
保護安全與隱私的最佳策略
Ensuring Compatibility with Managed Profiles
解決云同步的保存沖突
獲取位置更新
創(chuàng)建List
測試程序
管理網(wǎng)絡的使用情況
為App內容開啟深度鏈接
推薦TV內容
建立一個Notification
管理音頻播放
設計表盤
拍照
處理控制器輸入動作
判斷并監(jiān)測設備的底座狀態(tài)與類型
處理查詢的結果
保存到數(shù)據(jù)庫
支持多個游戲控制器
創(chuàng)建 Stub Content Provider
使得ListView滑動順暢
處理數(shù)據(jù)層的事件
創(chuàng)建TV應用的第一步
使得你的App內容可被Google搜索
將 Notification 放成一疊
創(chuàng)建 Stub 授權器
暫停與恢復Activity
管理設備的喚醒狀態(tài)
Android圖像與動畫
打印照片
云同步
創(chuàng)建TV直播應用
為Notification賦加可穿戴特性
提供一個Card視圖
建立請求隊列(RequestQueue)
適配不同的語言
創(chuàng)建詳情頁
測試UI組件
接收其他設備的文件
創(chuàng)建自定義View
建立第一個App
創(chuàng)建2D Picker
監(jiān)測電池的電量與充電狀態(tài)
打印自定義文檔
抽象出新的APIs
通知提示用戶
獲取文件信息
運用投影與相機視角
在IntentService中執(zhí)行后臺任務
多線程操作
創(chuàng)建一個Fragment
添加Action按鈕
在不同的 Android 系統(tǒng)版本支持控制器
維護兼容性
發(fā)送文件給其他設備
創(chuàng)建TV游戲應用
創(chuàng)建自定義的View類
代碼性能優(yōu)化建議
Intent過濾
適配不同的屏幕

在不同的 Android 系統(tǒng)版本支持控制器

編寫:heray1990 - 原文:http://developer.android.com/training/game-controllers/compatibility.html

如果我們正為游戲提供游戲控制器的支持,那么我們需要確保我們的游戲對于運行著不同 Android 版本的設備對控制器都有一致的響應。這會使得我們的游戲擴大用戶群體,同時,我們的玩家可以享受即使他們切換或者升級 Android 設備的時候,都可以使用他們的控制器無縫對接的游戲體驗。

這節(jié)課展示了如何用向下兼容的方式使用 Android 4.1 或者更高版本中可用的 API,使我們的游戲運行在 Android 2.3 或者更高的設備上時,支持下面的功能:

  • 游戲可以檢測是否有一個新的游戲控制器接入、變更或者移除。
  • 游戲可以查詢游戲控制器的兼容性。
  • 游戲可以識別從游戲控制器傳入的動作事件。

這節(jié)課的例子是基于 ControllerSample.zip 提供的參考實現(xiàn)。這個示例介紹了如何實現(xiàn) InputManagerCompat 接口來支持不同的 Android 版本。我們必須使用 Android 4.1(API level 16)或者更高的版本來編譯這個示例代碼。一旦編譯完成,生成的示例 app 可以在任何運行著 Android 2.3(API level 9)或者更高版本的設備上運行。

準備支持游戲控制器的抽象 API

假設我們想確定在運行著 Android 2.3(API level 9)的設備上,游戲控制器的連接狀態(tài)是否發(fā)生改變。無論如何,API 只在 Android 4.1(API level 16)或者更高的版本上可用,所以我們需要提供一個支持 Android 4.1(API level 16)或者更高版本的實現(xiàn)方法的同時,提供一個支持從 Android 2.3 到 Android 4.0 的回退機制。

為了幫助我們確定哪個功能需要這樣的回退機制,table 1 列出了 Android 2.3(API level 9)、3.1(API level 12)和 4.1(API level 16)之間,對于支持游戲控制器的不同之處。

Table 1. API 在不同 Android 版本間對游戲控制器支持的不同點

Controller Information Controller API API level 9 API level 12 API level 16
Device Identification getInputDeviceIds() *
getInputDevice() *
getVibrator() *
SOURCE_JOYSTICK * *
SOURCE_GAMEPAD * *
Connection Status onInputDeviceAdded() *
onInputDeviceChanged() *
onInputDeviceRemoved() *
Input Event Identification D-pad press ( KEYCODE_DPAD_UP, KEYCODE_DPAD_DOWN, KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_CENTER) * * *
Gamepad button press ( BUTTON_A, BUTTON_B, BUTTON_THUMBL, BUTTON_THUMBR, BUTTON_SELECT, BUTTON_START, BUTTON_R1, BUTTON_L1, BUTTON_R2, BUTTON_L2) * *
Joystick and hat switch movement ( AXIS_X, AXIS_Y, AXIS_Z, AXIS_RZ, AXIS_HAT_X, AXIS_HAT_Y) * *
Analog trigger press ( AXIS_LTRIGGER, AXIS_RTRIGGER) * *

我們可以使用抽象化概念來建立能夠工作在不同平臺的版本識別的游戲控制器支持。這種方法包括下面幾個步驟:

  1. 定義一個中間 Java 接口來抽象化我們游戲需要的游戲控制器功能的實現(xiàn)。

  2. 創(chuàng)建一個使用 Android 4.1 和更高版本 API 的接口的代理實現(xiàn)。

  3. 創(chuàng)建一個使用 Android 2.3 到 Android 4.0 之間可用的 API 的接口的自定義實現(xiàn)。

  4. 創(chuàng)建在運行時,在這上述這些實現(xiàn)之間切換的邏輯,并且開始使用我們游戲中的接口。

有關如何使用抽象化概念來保證應用可以在不同版本的 Android 之間,以向后兼容的方式工作的概述,請見創(chuàng)建向后兼容的 UI

添加向后兼容的接口

對于向后兼容,我們可以創(chuàng)建一個自定義接口,然后添加特定版本的實現(xiàn)。這種方法的一個優(yōu)點是它可以讓我們借鑒 Android 4.1(API level 16)上支持游戲控制器的公共接口。

// The InputManagerCompat interface is a reference example.
// The full code is provided in the ControllerSample.zip sample.
public interface InputManagerCompat {
    ...
    public InputDevice getInputDevice(int id);
    public int[] getInputDeviceIds();

    public void registerInputDeviceListener(
            InputManagerCompat.InputDeviceListener listener,
            Handler handler);
    public void unregisterInputDeviceListener(
            InputManagerCompat.InputDeviceListener listener);

    public void onGenericMotionEvent(MotionEvent event);

    public void onPause();
    public void onResume();

    public interface InputDeviceListener {
        void onInputDeviceAdded(int deviceId);
        void onInputDeviceChanged(int deviceId);
        void onInputDeviceRemoved(int deviceId);
    }
    ...
}

InputManagerCompat 接口提供了下面的方法:

getInputDevice()

借鑒 getInputDevice()。包括代表一個游戲控制器兼容性的 InputDevice 對象。

getInputDeviceIds()

借鑒getInputDeviceIds()。返回一個整型數(shù)組,每一個數(shù)組成員表示一個不同輸入設備的 ID。這對于想要構建一個支持多玩家和檢測連接了多少個控制器的游戲是很有用的。

registerInputDeviceListener()

借鑒registerInputDeviceListener()。注冊一個監(jiān)聽器,當一個新的設備添加、改變或者移除的時候,我們會收到通知。

unregisterInputDeviceListener()

借鑒unregisterInputDeviceListener()。注銷一個輸入設備監(jiān)聽器。

onGenericMotionEvent()

借鑒onGenericMotionEvent()。讓我們的游戲截取和處理 MotionEvent 對象和代表類似移動搖桿和按下模擬觸發(fā)器等事件的坐標值。

onPause()

當主 activity 暫?;蛘弋斢螒虿辉倬劢箷r,停止輪詢游戲控制器事件。

onResume()

當主 activity 恢復或者當游戲開始和在前臺運行時,啟動輪詢游戲控制器事件。

InputDeviceListener

借鑒 InputManager.InputDeviceListener 接口。當添加、改變或者移除游戲控制器時,會通知我們的游戲。

下一步,創(chuàng)建 InputManagerCompat 的實現(xiàn),使得可以在不同平臺版本間工作。如果我們的游戲運行在 Android 4.1 或者更高版本,調用 InputManagerCompat 方法,代理實現(xiàn)調用在 InputManager 中等效的方法。然而,如果我們的游戲運行在 Andoird 2.3 到 Android 4.0,自定義的實現(xiàn)過程通過使用不晚于 Android 2.3 引進的 API 來調用 InputManagerCompat 方法。不管在運行時使用哪種特定版本的實現(xiàn),實現(xiàn)會透明地將回調結果傳給游戲。

http://wiki.jikexueyuan.com/project/android-training-geek/images/backward-compatible-inputmanager.png" alt="" />

Figure 1. 接口和特定版本實現(xiàn)的類圖。

實現(xiàn) Android 4.1 和更高版本的接口

InputManagerCompatV16InputManagerCompat 接口的實現(xiàn),該接口代理方法調用一個 InputManagerInputManager.InputDeviceListenerInputManager是從系統(tǒng) Context 得到。

// The InputManagerCompatV16 class is a reference implementation.
// The full code is provided in the ControllerSample.zip sample.
public class InputManagerV16 implements InputManagerCompat {

    private final InputManager mInputManager;
    private final Map mListeners;

    public InputManagerV16(Context context) {
        mInputManager = (InputManager)
                context.getSystemService(Context.INPUT_SERVICE);
        mListeners = new HashMap();
    }

    @Override
    public InputDevice getInputDevice(int id) {
        return mInputManager.getInputDevice(id);
    }

    @Override
    public int[] getInputDeviceIds() {
        return mInputManager.getInputDeviceIds();
    }

    static class V16InputDeviceListener implements
            InputManager.InputDeviceListener {
        final InputManagerCompat.InputDeviceListener mIDL;

        public V16InputDeviceListener(InputDeviceListener idl) {
            mIDL = idl;
        }

        @Override
        public void onInputDeviceAdded(int deviceId) {
            mIDL.onInputDeviceAdded(deviceId);
        }

        // Do the same for device change and removal
        ...
    }

    @Override
    public void registerInputDeviceListener(InputDeviceListener listener,
            Handler handler) {
        V16InputDeviceListener v16Listener = new
                V16InputDeviceListener(listener);
        mInputManager.registerInputDeviceListener(v16Listener, handler);
        mListeners.put(listener, v16Listener);
    }

    // Do the same for unregistering an input device listener
    ...

    @Override
    public void onGenericMotionEvent(MotionEvent event) {
        // unused in V16
    }

    @Override
    public void onPause() {
        // unused in V16
    }

    @Override
    public void onResume() {
        // unused in V16
    }

}

實現(xiàn) Android 2.3 到 Android 4.0 的接口

InputManagerV9 實現(xiàn)使用了不晚于 Android 2.3 引進的 API。為了創(chuàng)建一個支持 Android 2.3 到 Android 4.0 的 InputManagerCompat 實現(xiàn),我們可以使用下面的對象:

  • 設備 ID 的 SparseArray 跟蹤已連接到設備的游戲控制器。
  • 一個 Handler 來處理設備事件。當一個 app 啟動或者恢復時,Handler 接收一個消息來開始輪詢游戲控制器的斷開。Handler 將啟動一個循環(huán)來檢查每個已知連接的游戲控制器并且查看是否返回一個設備 ID。返回 null 表示游戲控制器斷開。當 app 暫停時,Handler 停止輪詢。
  • 一個 InputManagerCompat.InputDeviceListenerMap 對象。我們會使用這個 listener 來更新跟蹤游戲遙控器的連接狀態(tài)。
// The InputManagerCompatV9 class is a reference implementation.
// The full code is provided in the ControllerSample.zip sample.
public class InputManagerV9 implements InputManagerCompat {
    private final SparseArray mDevices;
    private final Map mListeners;
    private final Handler mDefaultHandler;
    …

    public InputManagerV9() {
        mDevices = new SparseArray();
        mListeners = new HashMap();
        mDefaultHandler = new PollingMessageHandler(this);
    }
}

實現(xiàn)繼承 HandlerPollingMessageHandler,并重寫 handleMessage() 方法。這個方法檢查已連接的游戲控制器是否已經(jīng)斷開并且通知已注冊的 listener。

private static class PollingMessageHandler extends Handler {
    private final WeakReference mInputManager;

    PollingMessageHandler(InputManagerV9 im) {
        mInputManager = new WeakReference(im);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case MESSAGE_TEST_FOR_DISCONNECT:
                InputManagerV9 imv = mInputManager.get();
                if (null != imv) {
                    long time = SystemClock.elapsedRealtime();
                    int size = imv.mDevices.size();
                    for (int i = 0; i < size; i++) {
                        long[] lastContact = imv.mDevices.valueAt(i);
                        if (null != lastContact) {
                            if (time - lastContact[0] > CHECK_ELAPSED_TIME) {
                                // check to see if the device has been
                                // disconnected
                                int id = imv.mDevices.keyAt(i);
                                if (null == InputDevice.getDevice(id)) {
                                    // Notify the registered listeners
                                    // that the game controller is disconnected
                                    ...
                                    imv.mDevices.remove(id);
                                } else {
                                    lastContact[0] = time;
                                }
                            }
                        }
                    }
                    sendEmptyMessageDelayed(MESSAGE_TEST_FOR_DISCONNECT,
                            CHECK_ELAPSED_TIME);
                }
                break;
        }
    }
}

至于啟動和停止輪詢游戲控制器的斷開,重寫這些方法:

private static final int MESSAGE_TEST_FOR_DISCONNECT = 101;
private static final long CHECK_ELAPSED_TIME = 3000L;

@Override
public void onPause() {
    mDefaultHandler.removeMessages(MESSAGE_TEST_FOR_DISCONNECT);
}

@Override
public void onResume() {
    mDefaultHandler.sendEmptyMessageDelayed(MESSAGE_TEST_FOR_DISCONNECT,
            CHECK_ELAPSED_TIME);
}

重寫 onGenericMotionEvent() 方法檢測輸入設備是否已添加。當系統(tǒng)通知一個動作事件時,檢查這個事件是否從已經(jīng)跟蹤過的還是新的設備 ID 中發(fā)出。如果是新的設備 ID,通知已注冊的 listener。

@Override
public void onGenericMotionEvent(MotionEvent event) {
    // detect new devices
    int id = event.getDeviceId();
    long[] timeArray = mDevices.get(id);
    if (null == timeArray) {
        // Notify the registered listeners that a game controller is added
        ...
        timeArray = new long[1];
        mDevices.put(id, timeArray);
    }
    long time = SystemClock.elapsedRealtime();
    timeArray[0] = time;
}

listener 的通知通過使用 Handler 對象發(fā)送一個 DeviceEvent Runnable 對象到消息隊列來實現(xiàn)。DeviceEvent 包含了一個 InputManagerCompat.InputDeviceListener 的引用。當 DeviceEvent 運行時,適當?shù)?listener 回調方法會被調用,標志游戲控制器是否被添加、改變或者移除。

@Override
public void registerInputDeviceListener(InputDeviceListener listener,
        Handler handler) {
    mListeners.remove(listener);
    if (handler == null) {
        handler = mDefaultHandler;
    }
    mListeners.put(listener, handler);
}

@Override
public void unregisterInputDeviceListener(InputDeviceListener listener) {
    mListeners.remove(listener);
}

private void notifyListeners(int why, int deviceId) {
    // the state of some device has changed
    if (!mListeners.isEmpty()) {
        for (InputDeviceListener listener : mListeners.keySet()) {
            Handler handler = mListeners.get(listener);
            DeviceEvent odc = DeviceEvent.getDeviceEvent(why, deviceId,
                    listener);
            handler.post(odc);
        }
    }
}

private static class DeviceEvent implements Runnable {
    private int mMessageType;
    private int mId;
    private InputDeviceListener mListener;
    private static Queue sObjectQueue =
            new ArrayDeque();
    ...

    static DeviceEvent getDeviceEvent(int messageType, int id,
            InputDeviceListener listener) {
        DeviceEvent curChanged = sObjectQueue.poll();
        if (null == curChanged) {
            curChanged = new DeviceEvent();
        }
        curChanged.mMessageType = messageType;
        curChanged.mId = id;
        curChanged.mListener = listener;
        return curChanged;
    }

    @Override
    public void run() {
        switch (mMessageType) {
            case ON_DEVICE_ADDED:
                mListener.onInputDeviceAdded(mId);
                break;
            case ON_DEVICE_CHANGED:
                mListener.onInputDeviceChanged(mId);
                break;
            case ON_DEVICE_REMOVED:
                mListener.onInputDeviceRemoved(mId);
                break;
            default:
                // Handle unknown message type
                ...
                break;
        }
        // Put this runnable back in the queue
        sObjectQueue.offer(this);
    }
}

我們現(xiàn)在已經(jīng)有兩個 InputManagerCompat 的實現(xiàn):一個可以在運行 Android 4.1 或者更高版本的設備上工作,另一個可以在運行 Android 2.3 到 Android 4.0 的設備上工作。

使用特定版本的實現(xiàn)

特定版本切換的邏輯是在一個充當 factory 的類中實現(xiàn)。

public static class Factory {
    public static InputManagerCompat getInputManager(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            return new InputManagerV16(context);
        } else {
            return new InputManagerV9();
        }
    }
}

現(xiàn)在我們可以簡單地實例化一個 InputManagerCompat 對象,并且在主 View 中注冊 InputManagerCompat.InputDeviceListener。由于我們建立的版本切換邏輯,我們的游戲會自動為設備上運行的 Android 版本使用適當?shù)膶崿F(xiàn)。

public class GameView extends View implements InputDeviceListener {
    private InputManagerCompat mInputManager;
    ...

    public GameView(Context context, AttributeSet attrs) {
        mInputManager =
                InputManagerCompat.Factory.getInputManager(this.getContext());
        mInputManager.registerInputDeviceListener(this, null);
        ...
    }
}

下一步,重寫主 View 的 onGenericMotionEvent() 方法,詳見處理從游戲控制器傳來的 MotionEvent。我們的游戲現(xiàn)在應該可以一致地處理運行著 Android 2.3(API level 9)和更高版本設備上的游戲控制器事件。

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    mInputManager.onGenericMotionEvent(event);

    // Handle analog input from the controller as normal
    ...
    return super.onGenericMotionEvent(event);
}

我們可以在上述的 ControllerSample.zip 示例的 GameView 類中找到這個兼容性的完整的代碼。

上一篇:聯(lián)系人信息下一篇:緩存Bitmap