Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android SurfaceView游戲開發示例

Android SurfaceView游戲開發示例

編輯:Android開發教程

當我們需要開發一個復雜游戲的時候,而且對程序的執行效率要求很高時,View類就不能滿足需求了,這時必須用 SurfaceView類進行開發。

例如,對速度要求很高的游戲時,View類就不能滿足需求了,這時必須使用SurfaceView類進 行開發。例如,對速度要求很高的游戲,可以使用雙緩沖來顯示。游戲中的背景、人物、動畫等都需要繪制在一個畫布(Canvas) 上,而SurfaceView可以直接訪問一個畫布,SurfaceView 是提供給需要直接畫像素而不是使用窗體部件的應用使用的。 每個 Surface創建一個Canvas對象(但屬性時常改變),用來管理View和Surface上的繪圖操作。

具體示例:

Activity

public class Activity01 extends Activity {     
             
    GameSurfaceView gameView = null;     
             
    @Override 
    public void onCreate(Bundle savedInstanceState) {     
        super.onCreate(savedInstanceState);     
                
        gameView = new GameSurfaceView(this);     
        setContentView(gameView);     
    }     
}

GameSurfaceView

public class GameSurfaceView extends SurfaceView implements 

SurfaceHolder.Callback, Runnable{     
         
    private Activity01 mActivity = null;     
             
    private SurfaceHolder mHolder = null;     
    private Thread mThread = null;;     
    private boolean mLoop = false;     
             
    private Canvas mCanvas = null;     
    private Paint mPaint = null;     
    private long drawStartTime = 0, drawCostTime = 0, drawTotalTime = 30;     
             
    private float x, y;     
    private int screenWidth = 480, screenHeight = 800;     
             
    public GameSurfaceView(Context context) {     
        super(context);     

        mActivity = (Activity01)context;     

        mHolder = this.getHolder();     
        mHolder.addCallback(this);     
        setFocusable(true);     
    }     

    @Override 
    public void surfaceCreated(SurfaceHolder holder) {     
        mPaint = new Paint();     
        mPaint.setColor(Color.CYAN);     
        mLoop = true;     
                 
        new Thread(this).start();     
    }     

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {     
        screenWidth = width;     
        screenHeight = height;     

        x = screenWidth / 2;     
        y = screenHeight / 2;     
    }     

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) {     
        mLoop = false;     
    }     

    @Override 
    public void run() {     
        while(mLoop){     
            drawStartTime = SystemClock.uptimeMillis();     
                     
            synchronized (mHolder) {     
                try {     
                    mCanvas = mHolder.lockCanvas();     
                    onDraw();       // repaint     
                } catch (Exception e) {     
                    e.printStackTrace();     
                } finally {     
                    if(mCanvas != null){     
                        mHolder.unlockCanvasAndPost(mCanvas);     
                    }     
                }     
            }     

            drawCostTime = SystemClock.uptimeMillis() - drawStartTime;     
            Log.i("drawCostTime", "drawCostTime = " + drawCostTime);     
            try {     
                if(drawCostTime < drawTotalTime){     
                    Thread.sleep(drawTotalTime - drawCostTime);     // sleep elapse time     
                }     
            } catch (Exception e) {     
                e.printStackTrace();     
            }     
        }     
    }     

    private void onDraw(){     
        if(mHolder == null && mPaint == null){     
            return;     
        }     

        mPaint.setAntiAlias(true);     
        mPaint.setAlpha(100);     

        mPaint.setColor(Color.CYAN);    // paint background color     
        mCanvas.drawRect(0, 0, screenWidth, screenHeight, mPaint);     

        mPaint.setColor(Color.BLUE);    // paint foreground color     
        mCanvas.drawCircle(x, y, 50, mPaint);     
    }     

    @Override 
    public boolean onTouchEvent(MotionEvent event) {     
        super.onTouchEvent(event);     

        switch (event.getAction()) {     
        case MotionEvent.ACTION_DOWN:     
            this.x = event.getX();     
            this.y = event.getY();     
            break;     

        case MotionEvent.ACTION_MOVE:     
            this.x = event.getX();     
            this.y = event.getY();     
            break;     
         
        case MotionEvent.ACTION_UP:     
            this.x = event.getX();     
            this.y = event.getY();     
            break;     

        default:     
            this.x = event.getX();     
            this.y = event.getY();     
            break;     
        }     

        return true;    // true then event can be implemented again     
    }     

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event){     
        super.onKeyDown(keyCode, event);     

        if(keyCode == KeyEvent.ACTION_DOWN){     
            this.mActivity.finish();     
        }     

        return true;     
    }     
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved