How to calculate Android screen aspect ratio mathe

2019-04-05 02:22发布

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.

3条回答
家丑人穷心不美
2楼-- · 2019-04-05 02:43
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels
);
查看更多
够拽才男人
3楼-- · 2019-04-05 03:03

To find ration in (16:9) you need to find the GCD and then divide both numbers by that.

int gcd(int p, int q) {
    if (q == 0) return p;
    else return gcd(q, p % q);
}

void ratio(int a, int b) {
   final int gcd = gcd(a,b);
   if(a > b) {
       showAnswer(a/gcd, b/gcd);
   } else {
       showAnswer(b/gcd, a/gcd);
   }
}

void showAnswer(int a, int b) {
   System.out.println(a + " " + b);
}

After this just call ratio(1920,1080);

查看更多
可以哭但决不认输i
4楼-- · 2019-04-05 03:07

I think i am very much late for this answer but still for the people who want to know, the answer is:

if(screen_width > screen_height) 
{
    aspectRatio = screen_width / screen_height;
} 
else 
{
    aspectRatio = screen_height / screen_width;
}
查看更多
登录 后发表回答