嗨!我在水平滚动视图中有一个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 stubgetHolder().addCallback(this);canvasthread = new CanvasThread(getHolder(), this);setFocusable(true);
我试图改变
`synchronized (_surfaceHolder) { _panel.postInvalIDate(); }`
至
已同步(_surfaceHolder){
_panel.postInvalIDate();
}
我也尝试添加调用setwillNotDraw(false)而不走运:
@OverrIDepublic voID surfaceCreated(SurfaceHolder holder) { // Todo auto-generated method stub canvasthread.setRunning(true); canvasthread.start(); setwillNotDraw(false);
这似乎是一个常见问题,但是我遇到的所有解决方案都没有对我有用.
谢谢!
解决方法:
postInvalIDate不会使用surfaceVIEw调用onDraw.您需要解锁画布,绘制事物,然后锁定画布.这是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
总结 以上是内存溢出为你收集整理的未触发OnDraw(),surfaceView中未绘制任何内容-Android全部内容,希望文章能够帮你解决未触发OnDraw(),surfaceView中未绘制任何内容-Android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)