简体   繁体   中英

Java - I need help understanding UI scaling to different screen sizes

I am creating an Android app and want to know how to scale my images, buttons, etc. to fit the screen size. I am drawing my objects using RectF's and Canvas. I am running my screens through the View class and am not using XML. I have already posted on here but I am still very stuck on this issue and it is disabling me from releasing my app. If someone could give me some quick instruction over skype or teamview I would be so happy!

Thanks guys :)

EDIT

    titleBounds.set(screenWidth / 2 - screenWidth * 1/3 * density, 50 * density, screenWidth - titleBounds.left * density, titleBounds.top + screenHeight * 1/8 * density);

This is my rectF, I just want it to sit top middle on the home screen.

The density of a screen is determined by the amount of pixels and the physical size. A low density screen means there are not very many pixels per physical inch of screen. So something like a tablet, because of the big physical size, might have a low pixel density. New phones on the other hand, have a high number of pixels and yet a relatively small size screen, giving them a very high density.

You'll need to multiply your RectF values by the screen density, which will give you buttons etc that are approximately the same physical size across all screens. The screen density is a float value ranging from 0.75 (low density) to 4 (very very high density).

So if your button is about 1 inch wide on a phone, using the density multiplier will give you a button that is about 1 inch wide on a tablet as well.

You can get a density multiplier from the DisplayMetrics class: http://developer.android.com/reference/android/util/DisplayMetrics.html#density

You get the DisplayMetrics from the context resources:

getResources().getDisplayMetrics()

So your RectF would then be created as:

float density=getResources().getDisplayMetrics().density;

RectF bounds=new RectF(left*density, top*density, right*density, bottom*density);

There's a whole lot of information on density here: getting the screen density programmatically in android?

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