2012-04-27 85 views
-2

我正在製作一個簡單的繪畫應用程序,這是我第一次使用畫布。我已經在我的佈局xml中使用了SurfaceView,並且該ID是正確的。無論如何,這裏是我的代碼:Android帆布不顯示

package com.example.paint; 

import android.app.Activity; 
import android.graphics.Canvas; 
import android.os.Bundle; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class Paint extends Activity { 
/** Called when the activity is first created. */ 

SurfaceView v; 
SurfaceHolder holder; 
Canvas c; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    v = (SurfaceView) findViewById(R.id.svDraw); 
    holder = v.getHolder(); 

    while (true) { 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     if (!holder.getSurface().isValid()) 
      continue; 

     c = holder.lockCanvas(); 
     c.drawARGB(255, 0, 255, 0); 
     holder.unlockCanvasAndPost(c); 
    } 
} 
} 

謝謝。

+0

又有什麼問題屬於? – 2012-04-27 22:51:36

+3

你的應用程序將ANR,因爲你永遠在'onCreate'中阻止UI線程。查看[LunarLander](http://developer.android.com/resources/samples/LunarLander/src/com/example/android/lunarlander/index.html)Api演示,瞭解它的工作原理。 – zapl 2012-04-27 22:58:27

回答

2

你需要刪除while(true),它是一個無限循環,防止發生任何事情。

繪圖代碼在你ViewonDraw方法包含您的畫布對象