2010-12-27 4 views
4

J'utilise WallpaperManager.getDrawable() pour obtenir le fond d'écran en cours, puis le convertir en bitmap pour faire autre chose. Je trouve que parfois je vais obtenir les mauvaises données de fond d'écran lorsque l'appareil tourne en permanence. Par exemple, la largeur et la hauteur du papier peint concernent le mode portrait lorsque l'appareil est en mode paysage.Comment détecter l'orientation du papier peint dans Android

Est-ce que quelqu'un sait comment détecter l'orientation actuelle du papier peint ou des données connexes sur l'orientation du papier peint?

+0

Je ne sais pas si cela aide? – fuzz

Répondre

1

Je me rends compte que cette réponse est presque un an de retard, mais nous espérons que ce qui suit fournit une solution pour les autres qui tentent de déterminer l'orientation de leurs fonds d'écran:

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

le code ci-dessus retournera un entier qui est égal à Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, ou Surface.ROTATION_270.

Note: this fait référence au WallpaperService.

0

Ici, vous pouvez obtenir l'orientation donnée tout contexte:

@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 
} 
Questions connexes