我创建了我的第一个动态壁纸,在单独的线程中实现绘图。所以现在我有一个 WallpaperService 和我的 WallpaperPainter 来完成这项工作。问题是我在某些设备上获得了IllegalArgumentException
inunlockCanvasAndPost
方法(Samsung Note 就是其中之一)。我已经阅读了所有我能找到但无法修复该错误的建议。似乎unlockCanvasAndPost
在表面被破坏时调用了,因此画布无效。这是代码的基本部分:
在墙纸服务中:
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
super.onSurfaceChanged(holder, format, width, height);
painting.setSurfaceSize(width, height);
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
painting.start();
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
painting.stopPainting();
while (retry) {
try {
painting.join();
retry = false;
} catch (InterruptedException e) { }
}
super.onSurfaceDestroyed(holder);
}
在绘画线程中:
public void stopPainting() {
this.run = false;
synchronized(this) {
this.notify();
}
}
public void run() {
this.run = true;
Canvas c = null;
while (run) {
try {
synchronized (this) {
Thread.sleep(50);
c = this.surfaceHolder.lockCanvas();
doDraw(c);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (c != null) {
this.surfaceHolder.unlockCanvasAndPost(c); // << -- HERE IS THE PROBLEM
}
}
// if pause...
synchronized (this) {
if (wait) {
try {
wait();
} catch (Exception e) { }
}
}
}
}
谁能给我任何线索我做错了什么?我是 Java 和 Android 的新手。