Ho creato il mio primo wallpaper live che implementa il disegno in un thread separato. Così ora ho un WallpaperService e il mio WallpaperPainter che fa il lavoro. Il problema è che ottengo un IllegalArgumentException
nel metodo unlockCanvasAndPost
su alcuni dispositivi (Samsung Note è quello). Ho letto tutte le raccomandazioni che ho trovato ma non ho potuto correggere quell'errore. Sembra che lo unlockCanvasAndPost
venga chiamato quando la superficie viene distrutta, quindi la tela non è valida. Ecco le parti essenziali di codice:IllegalArgumentException in unlockCanvasAndPost (Android live wallpaper)
Nel servizio di carta da parati:
@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);
}
Nel thread dipinto:
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) { }
}
}
}
}
Qualcuno mi può dare alcuna idea di cosa sto facendo male? Sono nuovo sia per Java che per Android.