Android 台灣中文網

標題: 關於 SurfaceView [打印本頁]

作者: wjh1208    時間: 2014-12-23 14:45
標題: 關於 SurfaceView
小弟最近在研究  遊戲製作

看到了  SurfaceView的東西,有些原理不懂,請大大賜教

有個網站  (http://blog.csdn.net/xiaominghimi/article/details/6090575)

內有教學如下:




public class MySurfaceView extends SurfaceView implements Callback, Runnable {  
    private SurfaceHolder sfh;  
    private Thread th;  
    private Canvas canvas;  
    private Paint paint;  
    private int ScreenW, ScreenH;  
    public MySurfaceView(Context context) {  
        super(context);  
        th = new Thread(this);  
        sfh = this.getHolder();  
        sfh.addCallback(this);
        paint = new Paint();  
        paint.setAntiAlias(true);  
        paint.setColor(Color.RED);  
        this.setKeepScreenOn(true);
    }  
    @Override  
    public void startAnimation(Animation animation) {  
        super.startAnimation(animation);  
    }  
    public void surfaceCreated(SurfaceHolder holder) {  
        ScreenW = this.getWidth();
        ScreenH = this.getHeight();  
        th.start();  
    }  
    private void draw() {  
        try {  
            canvas = sfh.lockCanvas(); // 得到一个canvas实例  
            canvas.drawColor(Color.WHITE);// 刷屏  
            canvas.drawText("Himi", 100, 100, paint);// 画文字文本  
            canvas.drawText("这就是简单的一个游戏框架", 100, 130, paint);  
        } catch (Exception ex) {  
        } finally {
            if (canvas != null)  
                sfh.unlockCanvasAndPost(canvas);  // 将画好的画布提交  
        }  
    }   
    public void run() {  
        while (true) {  
            draw();  
            try {  
                Thread.sleep(100);  
            } catch (InterruptedException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
    }  
    public void surfaceChanged(SurfaceHolder holder, int format, int width,  
            int height) {  
    }  
    public void surfaceDestroyed(SurfaceHolder holder) {  
        // TODO Auto-generated method stub  
    }  
}  







我試著將 private void draw() 改成


  int X=100,Y=100;
Boolean test = true;

  private void draw() {  
        try {              
            canvas = sfh.lockCanvas(); // 得到一个canvas实例  
          if(test){
                           canvas.drawColor(Color.WHITE);// 刷屏  
                           canvas.drawText("Himi", X, Y, paint);// 画文字文本  
                           canvas.drawText("这就是简单的一个游戏框架", X, Y+30, paint);  
                            X+=30;
                    }

        } catch (Exception ex) {  
        } finally {
            if (canvas != null)  
                sfh.unlockCanvasAndPost(canvas);  // 将画好的画布提交  
        }  
    }   




當test  = ture時    畫面就順順的   文字向右移
當 test = false時   在我的認知上  應該是 文字停止不動
但事實上 他卻會左右移動呢???
已經完全沒有執行到 canvas.drawText  為何他畫面會改變???

麻煩大大解答.....謝謝





歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1