How to determine the orientation of the wallpaper in android

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

Does anyone know how to determine the current wallpaper orientation or any related wallpaper orientation data?

+4
source share
3 answers

I understand that this answer was almost a year late, but I hope the following gives a solution for others trying to determine the orientation of their wallpaper:

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

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

Note: this refers to WallpaperService .

+1
source

Here you can get the orientation specified by 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
source

Source: https://habr.com/ru/post/1333468/


All Articles