【发布时间】:2010-12-27 14:10:31
【问题描述】:
我使用WallpaperManager.getDrawable() 获取当前壁纸,然后将其转换为位图以执行其他操作。我发现有时我会在设备连续旋转时得到错误的壁纸数据。例如,当设备处于横向模式时,壁纸的宽度和高度约为纵向。
有谁知道如何检测壁纸的当前方向或有关壁纸方向的任何相关数据?
【问题讨论】:
标签: android orientation wallpaper
我使用WallpaperManager.getDrawable() 获取当前壁纸,然后将其转换为位图以执行其他操作。我发现有时我会在设备连续旋转时得到错误的壁纸数据。例如,当设备处于横向模式时,壁纸的宽度和高度约为纵向。
有谁知道如何检测壁纸的当前方向或有关壁纸方向的任何相关数据?
【问题讨论】:
标签: android orientation wallpaper
我意识到这个答案已经晚了将近一年,但希望以下内容可以为其他试图确定壁纸方向的人提供解决方案:
((WindowManager)
this.getApplication().getSystemService(Service.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
上面的代码将返回一个整数,它等于Surface.ROTATION_0、Surface.ROTATION_90、Surface.ROTATION_180 或Surface.ROTATION_270。
注意:this 指的是WallpaperService。
【讨论】:
在这里你可以得到给定任何上下文的方向:
@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
}
【讨论】: