2013-04-16 66 views
1

我正在使用自定義曲面視圖。但平局不出現,看法是全黑:自定義SurfaceView:不會出現

public class SV extends SurfaceView { 

    Context context; 

    public SV (Context a_context) 
    { 
     super(a_context); 
     context = a_context; 
    } 

    @Override 
    public void draw (Canvas c) 
    { 
     c.drawColor(0xFFFF0000); 
     Paint p = new Paint(); 
     p.setColor(0xFFFFFFFF); 
     c.drawCircle(c.getWidth()/2, c.getHeight()/2, 150, p); 
    } 

    @Override 
    public void onDraw (Canvas c) 
    { 
     c.drawColor(0xFF0088FF); 
    } 

} 

和活動的onCreate:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    SV sv = new SV (this); 
    setContentView(sv); 
} 

回答

0

添加setWillNotDraw(false)到語句到surfaceCreated()方法是從SurfaceHolder.Callback。所以你需要在你的Activity中添加下面的代碼。

public class CameraPreview extends Activity implements SurfaceHolder.Callback{ 

    private SurfaceHolder holder; 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     // your activity initiating code 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // Do somthing when surface changed. 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     // Do something when surface first created. 
     // add setWillNotDraw(false) to enable onDraw() and other functions 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // cleanup here 
    } 


} 
相關問題