1

我正在使用自定义表面视图。但是没有出现绘图并且视图是全黑的:

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);
}
4

1 回答 1

0

将语句添加setWillNotDraw(false)到 from 的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
    }


}
于 2013-04-16T04:39:52.650 回答