我正在尝试绘制从表面持有者的。lockCanvas()
函数返回的画布,但渲染的画布只是白色。
我的MySurface extends SurfaceView
和implements SurfaceHolder
构造函数:
MySurface(Context context){
super(context);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
displayThread = new MyDisplayThread(surfaceHolder);
}
我的surfaceCreated()
覆盖
public void surfaceCreated(SurfaceHolder sH){
displayThread.start();
}
我的surfaceDestroyed()
覆盖
public void surfaceDestroyed(SurfaceHolder sH){
boolean reset = true;
while(reset){
try{
displayThread.join();
reset = false;
}catch (InterruptedException e){}
}
}
我用于绘图的线程: 类变量是:
SurfaceHolder surfaceHolder,
Paint paint,
boolean isRunning,
Canvas canvas = null
class MyDisplayThread extends Thread {
MyDisplayThread(SurfaceHolder sH){
surfaceHolder = sH;
isRunning = true;
paint = new Paint();
}
@Override
public void run(){
while(isRunning){
canvas = surfaceHolder.lockCanvas();
try{
myDraw();
}finally {
if(canvas != null) surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
public void myDraw(){
paint.setARGB(255,152,54,12);
canvas.drawText("RSFDS",100,100,paint);
canvas.drawRect(120,150,300,300,paint);
paint.setARGB(255,255,255,255);
}
}
答案 0 :(得分:0)
尝试以下(过去这些对我有用):
将setWillNotDraw(false);
放入surfaceCreated
在绘制每个帧之前,在SurfaceHolder对象上调用postInvalidate();
。
答案 1 :(得分:0)
您需要在调用getHolder()
后再次致电surfaceCreated()
。这应该可以解决你的问题。
@Override
public void surfaceCreated(SurfaceHolder holder) {
surfaceHolder = getHolder();
}