我有一个扩展SurfaceView的类,我将持有人“ this.getHolder()”传递给了一个先前运行并正在运行的线程。该线程有一个循环,在该循环中,我想绘制一次背景,并在该背景上绘制其他位图:
public void run() {
try {
Paint p = new Paint();
p.setColor(Color.MAGENTA);
p.setStyle(Paint.Style.FILL);
Rect dty = null;
int i = 0;
while(!Thread.currentThread().isInterrupted()) {
if(Loader.getInstance().isPlaying()) {
// GAME LOOP HERE
// only performs drawing operations while surface is not destroying
synchronized (Loader.getInstance().getHolder()) {
if(Loader.getInstance().getHolder() != null) {
if(!Loader.getInstance().isBgDrawn()) {
dty = null;
c = Loader.getInstance().getHolder().lockCanvas(dty);
if(c != null) {
c.drawRGB(100,0,0);
Loader.getInstance().setBgDrawn(true);
Loader.getInstance().getHolder().unlockCanvasAndPost(c);
}
}
else {
dty = new Rect(0,0,200,200);
if(dty!=null) c = Loader.getInstance().getHolder().lockCanvas(dty);
if(c != null) {
if(dty!=null)c.drawRGB(0,0,100);
Loader.getInstance().getHolder().unlockCanvasAndPost(c);
}
}
}
}
}
Thread.sleep(1000);
}
throw new InterruptedException();
} catch (InterruptedException ie) {
// CLEAN THIS RUNNABLE HERE
Log.println(Log.ASSERT, TAG, "run: CLEANING GAME RUNNABLE");
}
}
但是,当背景不是背景(bgDrawn === true)时,holder.lockCanvas(dty)会忽略该dty并绘制整个表面。
我在lockCanvas(dty)内正确应用了脏矩形吗?只想绘制一次背景,然后在该背景上绘制一些小东西,然后在单独的线程中绘制所有内容。