2013-07-21 68 views
0
public class CannonView extends SurfaceView implements SurfaceHolder.Callback { 

    CannonThread cannonThread; 
    private Paint blockerPaint; 

    public CannonView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     blockerPaint = new Paint(); 
     blockerPaint.setStrokeWidth(10.0f); 
     getHolder().addCallback(this); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 

     cannonThread = new CannonThread(holder); 
     cannonThread.running(true); 
     cannonThread.start(); 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     boolean retry = true; 
     cannonThread.running(false); 
     while (retry) { 

      try { 
       cannonThread.join(); 
       retry = false; 
      } catch (InterruptedException e) { 

      } 

     } 

    } 

    private class CannonThread extends Thread { 

     boolean setRunning; 
     SurfaceHolder surfaceHolder; 

     public CannonThread(SurfaceHolder holder) { 
      setRunning = true; 
      surfaceHolder = holder; 

     } 

     public void running(boolean isRunning) { 
      setRunning = isRunning; 
     } 

     @Override 
     public void run() { 
      Canvas canvas = null; 

      while (setRunning) { 

       try { 

        canvas = surfaceHolder.lockCanvas(); 


         synchronized (surfaceHolder) { 

          canvas.drawLine(0, 0, 100, 100, blockerPaint); 

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

     } 

    } 
} 

雖然上面的代碼非常簡單 但它不是我的活動畫任何東西..
logcat的說,我做的主線程的工作太多了.. 62幀跳過了..
請幫忙表面觀...幀跳過消息

+0

你永遠不會調用的setColor上油漆。我不知道默認的alpha值是什麼。也許它是透明的。設置顏色AGRB,看看它是否繪製你的線。 – spartygw

+0

不,它沒有工作。它抱怨主線程52幀太多的工作跳過 –

回答

1
+0

謝謝你的鏈接,但你可以指出這裏錯了嗎??? –

+0

我想我找到了問題。在表面被破壞後,你的重試= false。這意味着while循環將停止。但是,然後再讓它變成真實的,這樣它就不斷循環。 – Nebraska

+0

等一下...我有完全相同的問題。當代碼中的某些內容持續循環時,就會發生這種情況。跟隨視頻,你應該沒問題。 – Nebraska