2013-05-17 12 views
0
public class MyBringBackSurface extends SurfaceView implements Runnable 
    { 
     SurfaceHolder ourHolder; 
     Thread ourThread; 
     boolean isRunning=false; 
     Bitmap[] bitmap={BitmapFactory.decodeResource(getResources(), R.drawable.dice1), 
       BitmapFactory.decodeResource(getResources(), R.drawable.dice2), 
       BitmapFactory.decodeResource(getResources(), R.drawable.dice3) 

     }; 

     public MyBringBackSurface(Context context) { 
      super(context); 
      ourHolder=getHolder(); 

     } 
     @Override 
     public void run() 
     { 

      while(isRunning) 
      { 
       if(!ourHolder.getSurface().isValid()) 
        continue; 
       Canvas canvas = ourHolder.lockCanvas(); 
       Bitmap[] bitmap={BitmapFactory.decodeResource(getResources(), R.drawable.dice1), 
         BitmapFactory.decodeResource(getResources(), R.drawable.dice2), 
         BitmapFactory.decodeResource(getResources(), R.drawable.dice3)}; 
       for(timer=0; timer< bitmap.length; timer++) 
       { 
        canvas.drawBitmap(bitmap[timer], 10,20,null); 
       } 
       ourHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
     public void pause() { 
      isRunning = false; 
      while(true) 
      { 
       try 
       { 
        ourThread.join(); 
       } 
       catch (InterruptedException e) 
       { 
        e.printStackTrace(); 
       } 
       break; 
      } 
      ourThread=null; 
     } 
     public void resume() 
     { 
      isRunning = true; 
      ourThread= new Thread(this); 
      ourThread.start(); 
     } 

    } 
} 
+0

您正在以相同的座標繪製圖像。這就是爲什麼你看到最後一次繪製canvas.drawBitmap(位圖[定時器],10,20,空); – Raghunandan

+0

請在文本正文中添加一個問題。你想要的是每個'bitmap []'條目要被順序繪製來模擬一個die roll? –

+0

@Raghunandan:是的,我知道我正在使用同一個座標..但實際上這是我的要求。其實我有一個骰子的形象。我想像他們畫的那樣骰子在滾動。 –

回答

0

try{ 
    Thread.sleep(1000);  
    canvas.drawBitmap(bitmap[timer], 10,20,null); 
    }catch(InterruptedException e){ 
} 

它符合你的答案嗎?

相關問題