4

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

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

4

3 回答 3

1

我意识到这个答案已经晚了将近一年,但希望以下内容可以为其他试图确定壁纸方向的人提供解决方案:

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

上面的代码将返回一个整数,它等于Surface.ROTATION_0Surface.ROTATION_90Surface.ROTATION_180Surface.ROTATION_270

注:thisWallpaperService.

于 2011-08-10T20:59:48.043 回答
0

不确定这是否有帮助?

进度对话框和后台线程处于活动状态时如何处理屏幕方向更改?

于 2011-01-05T14:18:31.550 回答
0

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
}
于 2018-02-12T21:06:53.760 回答