如何把J2ME游戏移植到Android平台

    技术2025-07-17  12

    本文主要介绍如何把J2ME游戏移植到Android平台的方法,如果你是个J2ME的游戏开发者,并且想把一些J2ME游戏快速地迁移到Android平台,那么相信本文会对你有所帮助。1.    平台比较J2me:    开发平台Android:    操作系统2.    工程结构比较(源代码,资源文件夹,图片,数据)J2me:    Res:资源文件    Src:源代码Android:    Src:源代码    Res/drawable:图片    Res/raw:声音    Res/values:字符串    Assets:数据文件3.    安装包比较J2me:    Jad,jarAndroid:    apk4.    代码结构比较J2me:    MIDlet,CanvasAndroid:    Activity,View都采用继承的方式,都只有一个MIDlet/Activity,一般都只有一个Canvas/View5.    代码细节比较l 全屏设置J2me:    在Canvas类中调用SetFullScreenMode(Boolean)Android:    在Activity类中调用

        //设定全屏显示   

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,         

                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

        requestWindowFeature(Window. FEATURE_NO_TITLE); 2 获得屏幕尺寸 J2me:     Canvas类的getHeight()和getWidth()方法 Android:

        int screenWidth,screenHeight;

        WindowManager windowManager = getWindowManager();

        Display display = windowManager.getDefaultDisplay();       screenWidth = display.getWidth();       screenHeight = display.getHeight();

    3 DisplayJ2me:    Display dis=Display.getDisplay(MIDlet);Android:    Display display = windowManager.getDefaultDisplay();4 画布类J2me:    CanvasAndroid:    继承View类,定义构造方法:    public MyView(Context context)    {        super(context);    }5 屏幕绘制方法J2me:    Paint(Graphics)Android:    void onDraw(Canvas g)6 GraphicsJ2me:Android:7 Image的创建J2me:    Image.createImage(path);Android:    img = BitmapFactory.decodeResource(getResources(),R.drawable.map0);8 Font的创建,Font使用,字体设置J2me:Android:9 drawImageJ2me:Android:    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint)    多个重载10 字符串绘制J2me:Android:    public native void drawText(String text, float x, float y, Paint paint);    多个重载11 setClipJ2me:Android:    public boolean clipRect(float left, float top, float right, float bottom,Region.Op op)    最后一个参数为:Region.Op.REPLACE12旋转J2me:    drawRegion()    drawImage() Android:    还没找到好的方法,不过可以先创建一张翻转后的图片,再使用,封装好的代码如下:    //创建翻转图片public Bitmap createTransImage(Bitmap img,int trans){//   Bitmap img;try{//       img = BitmapFactory.decodeResource(getResources(),sImg);int width = img.getWidth();int height = img.getHeight();int newWidth = 200;int newHeight = 200;// calculate the scale - in this case = 0.4ffloat scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// createa matrix for the manipulationMatrix matrix = new Matrix();// resize the bit mapmatrix.postScale(scaleWidth, scaleHeight);// rotate the Bitmapint degree=0;Bitmap resizedBitmap=null;int data[];int buf;switch(trans){case ROTATE_HOR://创建镜像翻转data=new int [img.getWidth()*img.getHeight()];img.getPixels(data, 0, img.getWidth(), 0, 0, img.getWidth(),img.getHeight());//交换数据for(int i=0;i<img.getHeight();i++)for(int j=0;j<img.getWidth()/2;j++){buf=data[i*img.getWidth()+j];data[i*img.getWidth()+j]=data[img.getWidth()*(i+1)-(j+1)];data[img.getWidth()*(i+1)-(j+1)]=buf;}resizedBitmap=Bitmap.createBitmap(data, img.getWidth(),img.getHeight(), Bitmap.Config.ARGB_4444);;return resizedBitmap;case ROTATE_VER://创建镜像翻转data=new int [img.getWidth()*img.getHeight()];img.getPixels(data, 0, img.getWidth(), 0, 0, img.getWidth(),img.getHeight());//交换数据for(int i=0;i<img.getHeight()/2;i++)for(int j=0;j<img.getWidth();j++){buf=data[i*img.getWidth()+j];data[i*img.getWidth()+j]=data[(img.getHeight()-i-1)*img.getWidth()+j];data[(img.getHeight()-i-1)*img.getWidth()+j]=buf;}resizedBitmap=Bitmap.createBitmap(data, img.getWidth(),img.getHeight(), Bitmap.Config.ARGB_4444);;return resizedBitmap;case ROTATE_90:matrix.postRotate(90);// recreate the new BitmapresizedBitmap = Bitmap.createBitmap(img, 0, 0,width, height, matrix, true);return resizedBitmap;case ROTATE_180:matrix.postRotate(180);// recreate the new BitmapresizedBitmap = Bitmap.createBitmap(img, 0, 0,width, height, matrix, true);return resizedBitmap;case ROTATE_270:matrix.postRotate(270);// recreate the new BitmapresizedBitmap = Bitmap.createBitmap(img, 0, 0,width, height, matrix, true);return resizedBitmap;}return resizedBitmap;}catch (Exception e){return null;}}13 drawRectJ2me:Android:    public void drawRect(float left, float top, float right, float bottom,Paint paint)14 声音处理J2me:Android:    创建:    MediaPlayer coverSound =MediaPlayer.create(Activity,R.raw.back);    coverSound.prepare();    播放:    coverSound.start();    暂停:    coverSound.pause();    声音设置:    AudioManager vc = (AudioManager)Activity.getSystemService(Context.AUDIO_SERVICE);    vc.adjustVolume(AudioManager.ADJUST_LOWER, 1);    关闭:    coverSound.stop();    coverSound.release();  15 中断处理J2me:Android:    public void onWindowFocusChanged(boolean visibility)16 填充屏幕J2me:Android:    CanvasInstance.drawColor(int color)l7 按键处理J2me:Android:    要使按键可以被响应,需要在构造函数中调用    this.setFocusable(true);    处理方法:    public boolean onKeyDown(int keyCode, KeyEvent msg)    public boolean onKeyUp(int keyCode, KeyEvent msg)18 触摸屏处理J2me:Android:    public boolean onTouchEvent(MotionEvent me)    {      if(me.getAction()==MotionEvent.ACTION_DOWN)          。。。。。。      else if(me.getAction()==MotionEvent.ACTION_UP)          。。。。。。      return true;    }

    19 资源文件的的存放位置及读取

    J2me:

    Android:     图片:         Res/drawable:     声音:         Res/raw     数据:         Asserts         InputStream is=ActivityInstance.getAssets().open(path + ".dat");

    20 屏幕刷新,

    J2me:

        repaint() Android:     postInvalidate(); 21 颜色的使用

    J2me:

    Android:       PaintInstance.setColor(0xAARRGGBB); 22 数据保存和读取 J2me: Android:     保存:       SharedPreferences settings = ActivityInstance.getSharedPreferences(String name,int mode);       SharedPreferences.Editor editor = settings.edit();       editor.putBoolean("hasRec", true);       。。。。。       editor.putInt("ex",enemy .i_X_abs);      editor.commit();    读取:      SharedPreferences settings = ActivityInstance.getSharedPreferences                                                       (String name,int mode);      this.curFaceDirection=settings.getInt("cd", 0);23 填充方式J2me:Android:    paint.setStyle(Style.FILL);    paint.setStyle(Style.STROKE);24锚点J2me:Android:    setTextAlign(Paint.Align.LEFT);25 连接处理J2me:    HttpConnection conn = (HttpConnection) Connector.open("www.baidu.com", Connector.READ, true);    conn.setRequestMethod("GET");    conn.setRequestProperty("accept", "**");    String location = conn.getRequestProperty("location");    int resCode = conn.getResponseCode();    InputStream stream = conn.getInputStream();    conn.disconnect();总结了一下,有以下几点不同之处:J2ME中的连接从Connector打开,Android中从URL对象打开要设置连接是否可读写,J2ME中可以直接在Connector.Open时设置,而在Android中必须使用setDoInput(boolean)和setDoOutput(boolean)方法设置在J2ME中可以在Connector.Open中对连接进行超时设置,在Android中使用setConnectTimeout(int)不仅可以对连接超时进行设置,还能设置超时时间,参数为0时忽略连接超时

    在使用这些Api时,一定要注意每个参数的意义,比如j2me中drawRect的后两个参数为宽度和高度,而在Android中则变成了结束点的坐标,使用时千万不能想当然的随意传参。

    对于Override方法的定义,一定别忘了super.的方式来进行回调。

    上面基本上把J2ME和Android在2D游戏游戏开发中常用的API做了一个比较,了解这些内容后,基本上是可以比较容易地把ME的游戏游戏平顺地迁 移到Android平台。当然,此处只限制为游戏,如果你想把一款J2ME的软件迁移到Android平台,此方法并不适用,你需要学习android的 控件的使用。

    最新回复(0)