如何在android中检测壁纸的方向

发布于 2024-10-09 11:17:04 字数 156 浏览 9 评论 0原文

我使用 WallpaperManager.getDrawable() 获取当前壁纸,然后将其转换为位图以执行其他操作。我发现当设备连续旋转时有时会得到错误的壁纸数据。例如,当设备处于横向模式时,壁纸的宽度和高度约为纵向。

有谁知道如何检测壁纸的当前方向或有关壁纸方向的任何相关数据?

I use WallpaperManager.getDrawable() to get the current wallpaper and then convert it to bitmap to do something else. I find that sometimes I will get the wrong data of wallpaper when the device rotates continuously. For example, the width and height of wallpaper is about portrait when the device is in the landscape mode.

Does anyone know how to detect the current orientation of wallpaper or any related data about wallpaper orientation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖凝香 2024-10-16 11:17:04

我意识到这个答案几乎晚了一年,但希望以下内容能为其他尝试确定壁纸方向的人提供解决方案:

((WindowManager) 
this.getApplication().getSystemService(Service.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();

上面的代码将返回一个等于 Surface.ROTATION_0 的整数、Surface.ROTATION_90Surface.ROTATION_180Surface.ROTATION_270

注意:this 指的是WallpaperService

I realize that this answer is almost a year late, but hopefully the following provides a solution for others trying to determine the orientation of their wallpapers:

((WindowManager) 
this.getApplication().getSystemService(Service.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();

the above code will return an integer which is equal to Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270.

Note: this refers to the WallpaperService.

溺深海 2024-10-16 11:17:04

在这里您可以获得给定任何上下文的方向:

@JvmStatic
fun isInPortraitMode(activity: Activity): Boolean {
    val currentOrientation = getCurrentOrientation(activity)
    return currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || currentOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@JvmStatic
fun getCurrentOrientation(context: Context): Int {
    //code based on https://www.captechconsulting.com/blog/eric-miles/programmatically-locking-android-screen-orientation
    val windowManager = context.getSystemService(Service.WINDOW_SERVICE) as WindowManager
    val display = windowManager.defaultDisplay
    val rotation = display.rotation
    val size = Point()
    display.getSize(size)
    val result: Int//= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
        // if rotation is 0 or 180 and width is greater than height, we have
        // a tablet
        if (size.x > size.y) {
            if (rotation == Surface.ROTATION_0) {
                result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
            }
        } else {
            // we have a phone
            if (rotation == Surface.ROTATION_0) {
                result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
            }
        }
    } else {
        // if rotation is 90 or 270 and width is greater than height, we
        // have a phone
        if (size.x > size.y) {
            if (rotation == Surface.ROTATION_90) {
                result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
            }
        } else {
            // we have a tablet
            if (rotation == Surface.ROTATION_90) {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }
        }
    }
    return result
}

Here you can get the orientation given any Context:

@JvmStatic
fun isInPortraitMode(activity: Activity): Boolean {
    val currentOrientation = getCurrentOrientation(activity)
    return currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || currentOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@JvmStatic
fun getCurrentOrientation(context: Context): Int {
    //code based on https://www.captechconsulting.com/blog/eric-miles/programmatically-locking-android-screen-orientation
    val windowManager = context.getSystemService(Service.WINDOW_SERVICE) as WindowManager
    val display = windowManager.defaultDisplay
    val rotation = display.rotation
    val size = Point()
    display.getSize(size)
    val result: Int//= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
        // if rotation is 0 or 180 and width is greater than height, we have
        // a tablet
        if (size.x > size.y) {
            if (rotation == Surface.ROTATION_0) {
                result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
            }
        } else {
            // we have a phone
            if (rotation == Surface.ROTATION_0) {
                result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
            }
        }
    } else {
        // if rotation is 90 or 270 and width is greater than height, we
        // have a phone
        if (size.x > size.y) {
            if (rotation == Surface.ROTATION_90) {
                result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
            }
        } else {
            // we have a tablet
            if (rotation == Surface.ROTATION_90) {
                result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
            } else {
                result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }
        }
    }
    return result
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文