2012-03-23 71 views
1

一直在尋找一些教程繪製使用SurfaceView帆布,但顯示出來的僅僅是一個黑色的背景。SurfaceView實現Runnable - 線程沒有啓動

public class FighterActivity extends Activity implements OnTouchListener { 
    /** Called when the activity is first created. */ 
    SurfaceController surface; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     surface = new SurfaceController(this); 
     surface.setOnTouchListener(this); 
     setContentView(surface); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     surface.pause(); 
    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     surface.resume(); 
    } 

    public class SurfaceController extends SurfaceView implements Runnable{ 

     Thread thread = null; 
     SurfaceHolder holder; 

     public SurfaceController(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
      holder = getHolder(); 
      System.out.println("HERE"); 
     } 

     public void run() { 
      // TODO Auto-generated method stub 
      System.out.println("Hello World2"); 
      while(true){ 
       if(!holder.getSurface().isValid()){ 
        System.out.println("NOT VALID"); 
        continue; 
       } 
       System.out.println("VALID!"); 
       Canvas can = holder.lockCanvas(); 
       can.drawARGB(255, 150, 150, 0); 
       holder.unlockCanvasAndPost(can); 
      } 
     } 

     public void pause(){ 

     } 

     public void resume(){ 

     } 

    } 

    public boolean onTouch(View view, MotionEvent me) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 

它得到System.out.println(「HERE」);這裏打印出來,但僅此而已happends,換句話說線程不上手,因爲「你好World2」不能打印,是什麼問題?

感謝所有幫助

+0

你沒有顯示你是如何使用*它的。通過它的聲音,我們在調用構造函數,但是然後呢? – 2012-03-23 08:10:31

+0

添加的代碼現在 – Araw 2012-03-23 08:17:19

回答

3

我假設你正在構建關閉的這一點:http://android-coding.blogspot.ca/2011/05/drawing-on-surfaceview.html

你(分別爲resumepauseSurfaceController)注意到那裏的onResumeMySurfaceViewonPauseMySurfaceView啓動實際的線程。您也需要在代碼中執行此操作,例如在SurfaceController

protected boolean running = false; 

public void resume() { 
    running = true; 
    thread = new Thread(this); 
    thread.start(); 
} 
+0

啊哈,當然,這就是它!非常感謝! :d – Araw 2012-03-23 08:31:03