屏幕方向更改时 lockCanvas 失败

lockCanvas failing when screen orientation changes

我正在尝试在 Android 中制作动态壁纸,但是当方向改变时应用程序崩溃了。尝试在表面支架上锁定画布时看起来它崩溃了,但我不确定我能做些什么来防止它。

这是 class:

public class LiveWallpaperService extends WallpaperService
{
    public void onCreate() {
        super.onCreate();
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public Engine onCreateEngine() {
        return new MyWallpaperEngine();
    }

    class MyWallpaperEngine extends Engine
    {
        private final Handler handler = new Handler();
        private final Runnable drawRunner = new Runnable() {
            @Override
            public void run() {
                draw();
            }
        };
        private boolean visible = true;

        Paint paint;

        MyWallpaperEngine() {
            paint = new Paint();
        }

        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            this.visible = visible;

            if (visible) {
                handler.post(drawRunner);
            }
            else {
                handler.removeCallbacks(drawRunner);
            }
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            this.visible = false;
            handler.removeCallbacks(drawRunner);
        }

        public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) {
            draw();
        }

        void draw() {
            final SurfaceHolder holder = getSurfaceHolder();

            Canvas c = null;

            try {
                c = holder.lockCanvas();

                if (c != null) {
                    // Paint stuff here.
                }
            }
            finally {
                if (c != null) {
                    holder.unlockCanvasAndPost(c);
                }
            }

            handler.removeCallbacks(drawRunner);
            if (visible) {
                handler.postDelayed(drawRunner, 10);
            }
        }
    }
}

这是方向改变时发生的异常:

E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE) 
E/Surface: dequeueBuffer failed (No such device)
E/BaseSurfaceHolder: Exception locking surface
   java.lang.IllegalArgumentException
           at android.view.Surface.nativeLockCanvas(Native Method)
           at android.view.Surface.lockCanvas(Surface.java:318)
           at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:194)
           at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:158)
           at android.service.wallpaper.WallpaperService$Engine.lockCanvas(WallpaperService.java:262)
           at greencell.bitpatternswallpaper.LiveWallpaperService$MyWallpaperEngine.draw(LiveWallpaperService.java:206)
           at greencell.bitpatternswallpaper.LiveWallpaperService$MyWallpaperEngine.run(LiveWallpaperService.java:51)
           at android.os.Handler.handleCallback(Handler.java:790)
           at android.os.Handler.dispatchMessage(Handler.java:99)
           at android.os.Looper.loop(Looper.java:164)
           at android.app.ActivityThread.main(ActivityThread.java:6494)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

更新:

我已经检查了许多其他似乎有相同问题的线程,但到目前为止我唯一能做的就是将 unlockCanvasAndPostlockCanvas 包装在 try catch 中以忽略 IllegalArgumentException.

draw() 中,我会尝试将 handler.removeCallbacks(drawRunner); 移动到 try 块之前。可能是 onOffsetsChanged() 在方向更改时被调用,并且处理程序上的前一个线程可能尚未调用 unlockCanvasAndPost(c),这解释了为什么 lockCanvas() 出错在那时候。但是,如果您在此处发布的代码与您在本地 运行 的代码完全匹配,则情况不应如此,因为您没有覆盖 onOffsetsChanged().

您可以尝试的另一件事是覆盖 onSurfaceChanged() 并像这样清除处理程序队列:

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    handler.removeCallbacks(drawRunner);
    super.onSurfaceChanged(holder, format, width, height);
}

最终,我在网上阅读的所有关于 WallpaperService 的例子都有一个 try-finally 块和 lock/unlock canvas 逻辑,所以我不会担心。

试试 android:configChanges="orientation" <service android:name=".LiveWallpaperService" /> 清单

示例: <service android:name=".LiveWallpaperService" android:configChanges="orientation" />