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

作者:孙东风 2010-01-07(转载务必注明出处)

笔者的上篇文章《两个小时精通Android开发》中讲解了利用Android的接口规范开发J2MEWrap包,从而解决了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平台上并进行按键响应了:)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 8

打赏作者

dongfengsun

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值