我已經創建了我的第一個活動壁紙在一個單獨的線程中實現繪圖。所以現在我有一個WallpaperService和我的WallpaperPainter誰做這項工作。問題是我在某些設備上使用unlockCanvasAndPost
方法獲得IllegalArgumentException
方法(Samsung Note是其中的一種)。我讀過所有我能找到的推薦,但無法修復這個錯誤。似乎unlockCanvasAndPost
在表面被銷燬時調用,因此畫布無效。這裏是代碼的基本部分:IllegalArgumentException in unlockCanvasAndPost(android動態壁紙)
在壁紙服務:
@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都是新手。