How to mathematically calculate the aspect ratio of an Android screen

One of the properties of the device’s screen that Android allows you to request an application is aspect ratio. In the examples I saw, this property has only two values: long and notlong .

I am trying to redo the logic used by Android to classify a device as having one of two aspect ratios.

To get some official data for work, I referenced the values ​​provided by the device definitions included in the AVD Manager in Android Studio and combined them with my own calculations: Devices included in AVD Manager

The Published Ratio column shows the value retrieved from AVD Manager. Based on these results, I do not understand how the Nexus 5 and 6 are considered notlong , while the Galaxy S4 and Galaxy Nexus are considered long .

+6
source share
2 answers
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels ); 
+4
source

I think I'm very late to this answer, but still for people who want to know, the answer is:

 if(screen_width > screen_height) { aspectRatio = screen_width / screen_height; } else { aspectRatio = screen_height / screen_width; } 
0
source

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


All Articles