gpt4 book ai didi

android - OnDraw() 未触发,在 surfaceView 中未绘制任何内容 - Android

转载 作者:太空狗 更新时间:2023-10-29 12:56:33 29 4
gpt4 key购买 nike

嗨!我在水平 ScrollView 中有一个 surfaceView,我想用 onDraw() 调用填充图像。然而,没有绘制任何东西。我有一个类,其中绘图是从线程 CanvasThread 完成的。

public class PanelChart extends SurfaceView implements SurfaceHolder.Callback {
private CanvasThread canvasthread ;
public PanelChart(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
getHolder().addCallback(this);
canvasthread = new CanvasThread(getHolder(), this);
setFocusable(true);

我尝试过改变

`synchronized (_surfaceHolder) {
_panel.postInvalidate();
}`

同步(_surfaceHolder){
_panel.postInvalidate();
}

我也曾尝试添加调用 setWillNotDraw(false) 但运气不佳:

 @Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
canvasthread.setRunning(true);
canvasthread.start();
setWillNotDraw(false);

这似乎是一个常见问题,但我遇到的所有解决方案都不适合我。

谢谢!

最佳答案

postInvalidate 不会调用带有 surfaceView 的 onDraw。您需要解锁 Canvas ,绘制内容,然后锁定 Canvas 。以下是 surfaceView 的线程示例:

    class CanvasThread extends Thread {
private SurfaceHolder surfaceHolder;
private PanelChart panel;
private boolean run = false;

public CanvasThread(SurfaceHolder surfaceHolder, PanelChart panel) {
this.surfaceHolder = surfaceHolder;
this.panel = panel;
}

public void setRunning(boolean run) {
this.run = run;
}

public SurfaceHolder getSurfaceHolder() {
return surfaceHolder;
}

@Override
public void run() {
Canvas c;
while (run) {
c = null;

//limit the frame rate to maximum 60 frames per second (16 miliseconds)
timeNow = System.currentTimeMillis();
timeDelta = timeNow - timePrevFrame;
if ( timeDelta < 16){
try{
Thread.sleep(16 - timeDelta);
}catch(InterruptedException e){

}
}
timePrevFrame = System.currentTimeMillis();

try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
panel.onDraw(c); //draw canvas
computePhysics(); //calculate next frame
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c); //show canvas
}
}//try finally
} //while
}//run
}//thread

关于android - OnDraw() 未触发,在 surfaceView 中未绘制任何内容 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219473/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com