J2ME遊戲移植到Android平臺的方法

本文主要介紹如何把J2ME遊戲移植到Android平臺的方法,若是你是個J2ME的遊戲開發者,而且想把一些J2ME遊戲快速地遷移到Android平臺,那麼相信本文會對你有所幫助。固然,若是您非說能夠安裝個JVM來實現,那您能夠直接跳過本文了。android

因爲手比較懶,對J2ME的描述不是很詳細,還望多包涵。廢話很少說,請看下文:ide

 

1.    平臺比較函數

J2me:post

    開發平臺學習

Android:字體

    操做系統this

 

2.    工程結構比較(源代碼,資源文件夾,圖片,數據)操作系統

J2me:code

    Res:資源文件對象

    Src:源代碼

Android:

    Src:源代碼

    Res\drawable:圖片

    Res\raw:聲音

    Res\values:字符串

    Assets:數據文件

 

3.    安裝包比較

J2me:

    Jad,jar

Android:

    apk

 

4.    代碼結構比較

J2me:

    MIDlet,Canvas

Android:

    Activity,View

都採用繼承的方式,都只有一個MIDlet/Activity,通常都只有一個Canvas/View

 

5.    代碼細節比較

 

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 Display

J2me:

    Display dis=Display.getDisplay(MIDlet);

Android:

    Display display = windowManager.getDefaultDisplay();

 

4 畫布類

J2me:

    Canvas

Android:

    繼承View類,定義構造方法:

    public MyView(Context context)

    {

        super(context);

    }

5 屏幕繪製方法

J2me:

    Paint(Graphics)

Android:

    void onDraw(Canvas g)

 

6 Graphics

J2me:

Android:

 

7 Image的建立

J2me:

    Image.createImage(path);

Android:

    img = BitmapFactory.decodeResource(getResources(),R.drawable.map0);

 

8 Font的建立,Font使用,字體設置

J2me:

Android:

 

9 drawImage

J2me:

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 setClip

J2me:

Android:

    public boolean clipRect(float left, float top, float right, float bottom,Region.Op op)

    最後一個參數爲:Region.Op.REPLACE

 

12旋轉

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.4f

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

 

// createa matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// rotate the Bitmap

int 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 Bitmap

resizedBitmap = Bitmap.createBitmap(img, 0, 0,

width, height, matrix, true);

return resizedBitmap;

case ROTATE_180:

matrix.postRotate(180);

// recreate the new Bitmap

resizedBitmap = Bitmap.createBitmap(img, 0, 0,

width, height, matrix, true);

return resizedBitmap;

case ROTATE_270:

matrix.postRotate(270);

// recreate the new Bitmap

resizedBitmap = Bitmap.createBitmap(img, 0, 0,

width, height, matrix, true);

return resizedBitmap;

}

return resizedBitmap;

}

catch (Exception e)

{

return null;

}

}

 

13 drawRect

 J2me:

 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].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的 控件的使用。

出自:http://blog.sina.com.cn/shilverfox

相關文章
相關標籤/搜索