开发者

How to detect the orientation of wallpaper in android

开发者 https://www.devze.com 2023-02-01 21:38 出处:网络
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 w开发者_开发技巧hen t

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 w开发者_开发技巧hen 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?


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.


Not sure if this helps?

How to handle screen orientation change when progress dialog and background thread active?


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消