Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate Android screen aspect ratio mathematically

One of the device screen properties that Android lets an app query is it's aspect ratio. In the examples that I have seen this property seems to have only two values - long and notlong.

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

To get some official data to work with, I referred to the values provided by the device definitions included in the AVD Manager tool in Android Studio, and combined that with my own calculations: Devices included in AVD Manager

The column "Published Ratio" shows the value extracted from the AVD Manager. Based on these results, I am failing to understand how Nexus 5 and 6 are considered notlong while Galaxy S4 and Galaxy Nexus are considered long.

like image 735
Ravi Gupta Avatar asked Feb 16 '15 22:02

Ravi Gupta


People also ask

What is the formula for aspect ratio?

Lesson Summary. The aspect ratio of an image is the ratio of its width to its height. For instance, if a rectangle has an aspect ratio of 2:1, then it is twice as wide as it is tall. The aspect ratio formula is width divided by height: r=wh r = w h .

How do you calculate 16 9 aspect ratio?

FORMULA. If the width divided by the height equals 1.778, then the aspect ratio is 16:9 (1.78:1). If you know the width of an object but not the height, then you can find the 16:9 height by dividing width by 1.778.

How do you measure screen size on android?

1. Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size.


1 Answers

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels);
like image 144
VikasGoyal Avatar answered Oct 23 '22 19:10

VikasGoyal