2011-08-07 30 views
0

我使用視圖來創建一個簡單的遊戲,該視圖擴展了SurfaceView並使用線程在SurfaceView上繪製圖像。遊戲將有自己的線程(遊戲引擎)來繪製和更新drawable。當被鎖定的畫布不爲空時,Android SurfaceView顯示空白

我有3個類來實現這個,即BattleActivity,BattleView和BattleThread。 BattleActivity從另一個活動中調用。 BattleThread會調用BattleView.update()和BattleView.render()來完成這項工作。但我沒有看到任何工作。我知道這一切都可以通過記錄和調試(我已經嘗試),但我仍然不知道哪個是不正確的。任何人都在意幫助我?


public class BattleActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // Making it full screen 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    //... shortened 

    // Create the battle view 
    battleView = new BattleView(this); 
    battleView.setBackground(battleBackground); 
    setContentView(battleView); 
}} 

的BattleView:

public class BattleView extends SurfaceView implements SurfaceHolder.Callback{ 
//... shortened 

public BattleView(Context context) 
{ 
    super(context); 
    getHolder().addCallback(this); 
    // Surface has been created 
    battleThread = new BattleThread(getHolder(), this); 
    setFocusable(true); 
} 

public synchronized void render(Canvas canvas) 
{ 
    // this function is called when I do debugging, but no image is shown 
    // canvas and background is not null 
    canvas.drawBitmap(background, 0, 0, null); 
} 

public synchronized void update() 
{ 
    monsterState = leftMonster.update(); 
    //... shortened 
    rightMonster.update(); 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) 
{ 
    // Start the thread 
    battleThread.setRunning(true); 
    battleThread.start(); 
}} 

的BattleThread:

public class BattleThread extends Thread { 
public BattleThread(SurfaceHolder surfaceHolder, BattleView battleView) 
{ 
    super(); 
    this.surfaceHolder = surfaceHolder; 
    this.battleView = battleView; 
} 

@Override 
public void run() 
{ 
    Canvas canvas; 
    //... shortened 

    while (running) 
    { 
     canvas = null; 
     // try locking the canvas for exclusive pixel editing 
     // in the surface 
     try 
     { 
      canvas = this.surfaceHolder.lockCanvas(); 
      synchronized (surfaceHolder) 
      { 
       beginTime = System.currentTimeMillis(); 
       framesSkipped = 0; // resetting the frames skipped 
       // update game state 
       this.battleView.update(); 

       beginTime = System.currentTimeMillis(); 
       // render it 
       this.battleView.render(canvas);    

       //... shortened 
      } 
     } 
     finally 
     { 
      if (canvas != null) 
      { 
       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } // end finally 
    } 
} 

}

回答

1

我知道這是一箇舊的帖子,但以防萬一它幫助那裏的人。

我有同樣的問題,幾乎相同的代碼給你,並發現我所需要做的就是從我覆蓋的onDraw(Canvas canvas)方法中調用postInvalidate()在我的SurfaceView中。

這是我的代碼:

@Override 
protected void onDraw(Canvas canvas) 
{ 
    mPaperPlaneImage.draw(canvas); 
    postInvalidate(); 
} 
0

你需要調用的onDraw(帆布油畫)來呈現,而不是渲染(),因爲onDraw的作品無線硬件屏幕緩衝區。檢查第二個例子How can I use the animation framework inside the canvas?

+0

做到這一點,但它並沒有以某種方式工作。 :(目前試圖在BattleView中回滾我所有的代碼,看它是否有效。 – Febiyan