How to calculate Android screen aspect ratio mathematically

久未见 提交于 2019-12-04 01:48:38
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels
);
Patel Pinkal

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);

Ashwani

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!