简体   繁体   中英

Android how to evaluate view size to keep same proportion in all devices

I'm searching for a way to keep view size proportioned on all devices. I experimented with many maths calculations (eg mixing screen size, density, percentage, etc.) but haven't found any solutions, just different results for each device tested.

Let me present an example: Suppose I'm testing an app on a sw-360dp and eventually I find a size view that works.

relativeLayoutParams.width  = 150;

relativeLayoutParams.height = 250;

This has the effect of creating a view with size about 1/3 of screen width and 1/2 screen height.

How can I dynamically arrive at value for width and height, that lets the view keep the aspect ratio of 1/3 width to 1/2 height? At least I think that's what the documentation is suggesting when it says: "calculate exact view size (or margin, padding, etc.) from a percentage of screen size".

Or have I misinterpreted it?

Any help will be appreciated.

You can get the device screen size and there by define the ratio,

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

Then,

    width = (int) size.x * 1/3 ; 
    height = (int) size.y * 1/2 ; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM