我试图让应用程序每半秒在画布上绘制一些东西,但SurfaceHolder.getSurface().isValid()
返回false,当我调用SurfaceHolder.lockCanvas()
时,它返回null
根据this SO question,我应该使用SurfaceHolder.Callback.surfaceCreated
但是从不创建表面。
我的主onCreate
Activity
方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ChartPainter p = new ChartPainter(this);
}
我的ChartPainter.java
public ChartPainter(Context context) {
super(context);
holder = getHolder();
final boolean a[] = new boolean[1];
a[0] = false;
holder.addCallback(new Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
a[0] = true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
});
} [...]//more omitted code here
如何为SurfaceHolder
创建表面?
答案 0 :(得分:1)
ChartPainter是否扩展了SurfaceView?
注意:SurfaceHolder通常与SurfaceView一起使用。当活动到达前景并且其SurfaceView即将渲染时,WindowManager将要求SurfaceFlinger创建一个新曲面。然后将调用SurfaceHolder的surfaceCreated()。