android–>遊戲框架 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

說明一下:一直以來都是做j2me平臺的遊戲開發的。這次要轉到android ,想想肯定思想是想通的,因此也就寫瞭此文

 

 廢話不多說直接上源碼

 

 

 

package Sur.Face.Pro;

 

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.SurfaceHolder.Callback;

 

public class MySurfaceView extends SurfaceView implements Callback, Runnable {

       public SurfaceHolder sfh;

       public Canvas canvas;

       public Paint paint;

         public static int SC_W  , SC_H ;

       public MySurfaceView(Context context, AttributeSet attrs) {

              super(context, attrs);

              sfh = getHolder();

              sfh.addCallback(this); //binding surface to surface view

              paint = new Paint();

              paint.setAntiAlias(true);

                  paint.setColor(Color.RED);

                  this.setKeepScreenOn(true);// keep screen light

       }

 

       public void run() {

              int count = 0;

              while (count++ < 20) {

                     logic();

                     paint();

                     sleep();

              }

       }

 

       private void logic() {

              // do your logic

       }

 

       public void sleep() {

              try {

                     Thread.sleep(100);

              } catch (InterruptedException e) {

                     e.printStackTrace();

              }

       }

 

       public void paint() {

              canvas = sfh.lockCanvas();                    // 獲得類畫佈其他的操作就跟j2me是相同的瞭         // do your paint

 

              sfh.unlockCanvasAndPost(canvas);

 

       }

 

       public void surfaceCreated(SurfaceHolder holder) {

               SC_W = getWidth() ;

               SC_H = getHeight() ;

               new Thread(this).start();

 

       }

 

       public void surfaceChanged(SurfaceHolder holder, int format, int width,

                     int height) {

 

       }

 

       public void surfaceDestroyed(SurfaceHolder holder) {

 

       }

 

}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。