兩個(gè)小時(shí)精通Android開發(fā)之按鍵映射篇_第1頁
兩個(gè)小時(shí)精通Android開發(fā)之按鍵映射篇_第2頁
兩個(gè)小時(shí)精通Android開發(fā)之按鍵映射篇_第3頁
兩個(gè)小時(shí)精通Android開發(fā)之按鍵映射篇_第4頁
兩個(gè)小時(shí)精通Android開發(fā)之按鍵映射篇_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、筆者的上篇文章兩個(gè)小時(shí)精通Android開發(fā)中講解了利用Android的接口規(guī)范開發(fā)J2ME的Wrap包,從而解決了J2ME程序無法運(yùn)行在Android平臺(tái)上的技術(shù)難題,但是Android平臺(tái)和J2ME另一個(gè)重要的不同點(diǎn)就是按鍵響應(yīng)接口,如何讓J2ME程序的按鍵響應(yīng)可以無縫的移植到Android上呢?答案也是Wrap包。 筆者這里通過按鍵映射把Android平臺(tái)的鍵盤碼轉(zhuǎn)換成J2ME平臺(tái)的鍵盤碼,或者把Android平臺(tái)的觸摸事件通過代理傳遞給J2ME中相應(yīng)的接口。 首先,需要在Android平臺(tái)的GameCanvas里進(jìn)行如下定義: public class GameCanvas exten

2、ds Screen public static final int UP = 1; public static final int DOWN = 6; public static final int LEFT = 2; public static final int RIGHT = 5; public static final int FIRE = 8; public static final int GAME_A = 9; public static final int GAME_B = 10; public static final int GAME_C = 11; public stat

3、ic final int GAME_D = 12; public static final int KEY_NUM0 = 48; public static final int KEY_NUM1 = 49; public static final int KEY_NUM2 = 50; public static final int KEY_NUM3 = 51; public static final int KEY_NUM4 = 52; public static final int KEY_NUM5 = 53; public static final int KEY_NUM6 = 54; p

4、ublic static final int KEY_NUM7 = 55; public static final int KEY_NUM8 = 56; public static final int KEY_NUM9 = 57; public static final int KEY_STAR = 42; public static final int KEY_POUND = 35; 當(dāng)然,為了適配J2ME里的接口筆者還定義如下的接口: protected abstract void keyRepeated(int keyCode); protected abstract void keyP

5、ressed(int keyCode); protected abstract void keyReleased(int keyCode); protected abstract void pointerPressed(int x, int y); protected abstract void pointerReleased(int x, int y) protected abstract void pointerDragged(int x, int y); 筆者在上面定義的都是J2ME的視圖類Canvas里的按鍵響應(yīng)接口,這里需要做的工作就是把Android里的按鍵進(jìn)行映射轉(zhuǎn)換成J2ME里

6、的標(biāo)準(zhǔn)鍵值后傳遞給這些接口,并由繼承自GameCanvas的界面類進(jìn)行實(shí)現(xiàn),完全和J2ME里一樣了,基本不用修改任何代碼。 鍵值映射的代碼如下: public int keyActual = 0; public int keyAction = 0; public void keyPreparse(int keyCode,KeyEvent e) if(keyCode = KeyEvent.KEYCODE_0) keyActual = GameCanvas.KEY_NUM0; else if(keyCode = KeyEvent.KEYCODE_1) keyActual = GameCanvas.

7、KEY_NUM1; else if(keyCode = KeyEvent.KEYCODE_2) keyActual = GameCanvas.KEY_NUM2; else if(keyCode = KeyEvent.KEYCODE_3) keyActual = GameCanvas.KEY_NUM3; else if(keyCode = KeyEvent.KEYCODE_4) keyActual = GameCanvas.KEY_NUM4; else if(keyCode = KeyEvent.KEYCODE_5) keyActual = GameCanvas.KEY_NUM5; else i

8、f(keyCode = KeyEvent.KEYCODE_6) keyActual = GameCanvas.KEY_NUM6; else if(keyCode = KeyEvent.KEYCODE_7) keyActual = GameCanvas.KEY_NUM7; else if(keyCode = KeyEvent.KEYCODE_8) keyActual = GameCanvas.KEY_NUM8; else if(keyCode = KeyEvent.KEYCODE_9) keyActual = GameCanvas.KEY_NUM9; else if(keyCode = KeyE

9、vent.KEYCODE_POUND) keyActual = GameCanvas.KEY_POUND; else if(keyCode = KeyEvent.KEYCODE_STAR) keyActual = GameCanvas.KEY_STAR; else if(keyCode = KeyEvent.KEYCODE_DPAD_UP) keyActual = GameCanvas.UP; keyAction = GameCanvas.UP; else if(keyCode = KeyEvent.KEYCODE_DPAD_DOWN) keyActual = GameCanvas.DOWN;

10、 keyAction = GameCanvas.DOWN; else if(keyCode = KeyEvent.KEYCODE_DPAD_LEFT) keyActual = GameCanvas.LEFT; keyAction = GameCanvas.LEFT; else if(keyCode = KeyEvent.KEYCODE_DPAD_RIGHT) keyActual = GameCanvas.RIGHT; keyAction = GameCanvas.RIGHT; else if(keyCode = KeyEvent.KEYCODE_DPAD_CENTER) keyActual =

11、 GameCanvas.FIRE; keyAction = GameCanvas.FIRE; else if(keyCode = KeyEvent.KEYCODE_SOFT_LEFT) keyActual = Globe.softKeyLeft; else if(keyCode = KeyEvent.KEYCODE_SOFT_RIGHT) keyActual = Globe.softKeyRight; 筆者對(duì)Android里常用的鍵19以及*、#、上下左右、確定鍵進(jìn)行了映射,映射后為標(biāo)準(zhǔn)J2ME下的鍵值,接下來只需要把這些鍵值傳遞給原來J2ME里的接口即可,如下: public boolean

12、 onTouchUp(MotionEvent e) if (isEvent) return isEvent; pointerReleased(int)(e.getX(),(int)(e.getY(); return isEvent; public boolean onTouchDown(MotionEvent e) if (isEvent) return isEvent; pointerPressed(int)(e.getX(),(int)(e.getY(); return isEvent; public boolean onKeyDown(int keyCode, KeyEvent e) keyPreparse(keyCode,e); keyPressed(keyActual); return true; public boolean onKeyUp(int keyCode, KeyEvent e) keyPreparse(keyCode,e); keyReleased(keyActual); return true; 前面是對(duì)觸摸事件進(jìn)行的映射處理,后面是對(duì)按鍵事件進(jìn)行的處理,當(dāng)然,如果有需要還可

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論