0

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.

Divergent
  • 141
  • 2
  • 19

1 Answers1

1

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?

Community
  • 1
  • 1
Brian DeWolff
  • 326
  • 1
  • 5
  • And to keep the images the same scale so they dont get blurry, what do I need to do? Would you be willing to go on teamview to show a quick example? – Divergent Jun 15 '14 at 21:15
  • I understand whats going on somewhat but I just dont know how to apply it to my code. – Divergent Jun 15 '14 at 21:28
  • I added what I am doing to my code, If you could take a look at it – Divergent Jun 15 '14 at 22:25
  • You shouldn't be basing your dimensions on the screen size, that's already being handled by the density. It would be more clear if you broke it down something like this: `int titleTop=16; int titleHeight=48; int titleWidth=256; int titleLeft=screenWidth/2-titleWidth*density/2; titleBounds.set(titleLeft, titleTop*density, titleLeft+titleWidth*density, titleTop*density+titleHeight*density);` – Brian DeWolff Jun 15 '14 at 23:10
  • So I set an initial size and it will scale that size to fit all other screen sizes? – Divergent Jun 15 '14 at 23:23
  • Whatever numbers get you the size you want on the device you're developing on, should give you approximately that same physical size on other devices as well. – Brian DeWolff Jun 15 '14 at 23:25
  • Also how are you doing this titleWidth, titleHeight, titleTop, titleLeft? It goes by left, top, right, bottom doesnt it? – Divergent Jun 15 '14 at 23:27
  • Yes the Rect parameters are in that order. I'm using the width to calculate the left side such that the rectangle is centered. The order in which I declared those variables is irrelevant to the values that are used to create the rectangle. – Brian DeWolff Jun 15 '14 at 23:45
  • Which values do I apply the density variable to? All sides of a rect? – Divergent Jun 23 '14 at 15:09