两个小时精通Android开发之按键映射篇

kome2000 贡献于2012-01-30

作者 Administrator  创建于2010-07-19 02:51:00   修改者Administrator  修改于2010-07-19 02:52:00字数3745

文档摘要:笔者这里通过按键映射把Android平台的键盘码转换成J2ME平台的键盘码,或者把Android平台的触摸事件通过代理传递给J2ME中相应的接口。
关键词:

笔者的上篇文章《两个小时精通Android开发》中讲解了利用Android的接口规范开发J2ME的Wrap包,从而解决了J2ME程序无法运行在Android平台上的技术难题,但是Android平台和J2ME另一个重要的不同点就是按键响应接口,如何让J2ME程序的按键响应可以无缝的移植到Android上呢?答案也是Wrap包。 笔者这里通过按键映射把Android平台的键盘码转换成J2ME平台的键盘码,或者把Android平台的触摸事件通过代理传递给J2ME中相应的接口。 首先,需要在Android平台的GameCanvas里进行如下定义: public class GameCanvas extends 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 static 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; public 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; } 当然,为了适配J2ME里的接口笔者还定义如下的接口: protected abstract void keyRepeated(int keyCode); protected abstract void keyPressed(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里的按键响应接口,这里需要做的工作就是把Android里的按键进行映射转换成J2ME里的标准键值后传递给这些接口,并由继承自GameCanvas的界面类进行实现,完全和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.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 if(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 == KeyEvent.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; 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 = 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; } } 笔者对Android里常用的键1~9以及*、#、上下左右、确定键进行了映射,映射后为标准J2ME下的键值,接下来只需要把这些键值传递给原来J2ME里的接口即可,如下: public boolean 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; } 前面是对触摸事件进行的映射处理,后面是对按键事件进行的处理,当然,如果有需要还可以实现getGameAction()接口,如下: public int getKeyStates() { return keyActual; } public int getGameAction(int keyCode) { return keyAction; } 到这里,读者原来的J2ME程序基本不需要修改任何代码就可以运行在Android平台上并进行按键响应了:) 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dongfengsun/archive/2010/01/08/5159373.aspx

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 3 人已下载

下载文档