android - 动态壁纸屏幕旋转

标签 android live-wallpaper screen-rotation

我目前正在开发一个动态壁纸,它非常密集并且不能很好地处理屏幕旋转。

事实上,墙纸被破坏并显示空白屏幕,而没有调用 onSurfaceChanged!

这是我在 onSurfaceChanged 方法中的内容:

@Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        // TODO Auto-generated method stub
        super.onSurfaceChanged(holder, format, width, height);

        mRatio = (float) width / height;
        if (mRatio > 1) {
            orientation = 0;
        }
        if (mRatio < 1) {
            orientation = 1;
        }
        setRunning(true);
        Log.i(TAG, "Screen Rotation...");
        mHandle.post(r);
    }

我肯定此方法不会被调用,因为没有日志消息。

为什么会发生这种情况?处理屏幕旋转的技术有哪些? 难道是我的动态壁纸太密集了,虚空无法调用?

另外,onVisibilityChanged 也没有被调用,当我在模拟器上打开应用程序时,没有日志消息:

@Override
    public void onVisibilityChanged(boolean visible) {
        // TODO Auto-generated method stub
        super.onVisibilityChanged(visible);
        if (visible) {
            setRunning(true);
            Log.i(TAG, "Visible...");
            mHandle.postDelayed(r, 2000);
        } else {
            setRunning(false);
            Log.i(TAG, "Invisible...");
            mHandle.removeCallbacks(r);
        }
    }

最佳答案

在您的 list 中,声明:

    <activity android:name=".YourActivityName"
              android:configChanges="keyboardHidden|orientation"
    </activity>

您的 onSurfaceChanged - 方法只有在 list 中声明 configChanges - 属性时才会被调用!

关于您的第二个问题:onVisibilityChanged 不是您期望的名称:

当包含的窗口改变其可见性时调用(在 GONE、INVISIBLE 和 VISIBLE 之间)。请注意,这会告诉您您的窗口是否对窗口管理器可见;这不会告诉您您的窗口是否被屏幕上的其他窗口遮挡,即使它本身是可见的。

您需要通过 onPause()onResume() 检查您的应用是否对用户“可见”

关于android - 动态壁纸屏幕旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11071256/

相关文章:

java - Android 游戏(Llama 或 Duck)setContentView 上内存不足

Android Kotlin 协程单元测试

android - 如何调整视口(viewport)大小和缩放以支持跨浏览器?

android - 如何在 Android 动态壁纸上添加文字

安卓动态壁纸幻灯片动画

java - Android 和 Eclipse 的 Protocol Buffer : NoClassDefFoundError

android - 如何在 WallpaperService 中设置 webview 的大小?

ios - 旋转时更改自定义键盘的高度

Android Studio API 级别 28 : Rotating emulator does not rotate the app contents or device UI

android - 旋转回纵向时避免 Android VideoView 损坏