1

i'm developing a game for android based on the SurfaceView class. Currently i'm stuck trying to optimize the view for different screen-sizes.

Problem 1: i have PNG-Files in my res folder and i draw them using Canvas.drawBitmap. After reading this article ( http://developer.android.com/guide/practices/screens_support.html ) i thought android resizes the bitmaps if the screen is bigger. Unfortunately i recognized no scaling (or any changes) when launching my app on a tablet vs. launching it on a phone. So how is it done? Should i come up with some fancy strategy to calculate the available space and draw my bitmaps using the source- and destination-Rectangle? Would be nice if someone has a best practice or some hints how to draw bitmaps on a canvas. I already read the pixel<->dip conversion topics but i have to say i don't know how to use that "solution" in my app. So any help appreciated...

Problem 2: I have 3 PNGs in my res folder. One ldpi, one mdpi and one hdpi. Ldpi image has 50px*50px, mdpi has 75px*75px and hdpi has 100px*100px. When i load the app in the emulator (800*480) it uses the 100*100 png. When i load the app on the Acer Iconia Tab it uses the 75*75 image. I thought hdpi is for big screens (=tab) and mdpi or ldpi would be used for the emulator screen. At least i thought android would use the hdpi-image also on the tab but i got surprised... Any explantation for this as well much appreciated

Sorry 4 long text, just trying to be clear...

cya EffDee

2 Answers 2

1

For calculating how big your drawing should be, you can use Canvas.getDensity().

The DPI value of a screen depends on the amount of pixels and on the size of the screen. Tablets do have a high resolution, but most are quite large aswell. This leads to a lower DPI value. For more information have a look at http://developer.android.com/guide/practices/screens_support.html

2
  • thank you for your comment. i tried working with density but had no luck... i'm still bit confused... i got a Bitmap with a width(=w) and height(=h) and instead of drawing the bitmap like in my prototyp-verson to the point 10,10 with drawBitmap(myBitmap, 10, 10, null) you suggest i do something like drawBitmap(myBitmap, wholeBitmapRect, new Rect(10, 10, w*densityFactor, h*densityFactor), null) this is driving me crazy! thx for clearing that up with the dpi value... now it makes sense ;)
    – Effdee
    Nov 2, 2011 at 17:23
  • This post may be useful to you: stackoverflow.com/questions/5014882/…
    – Thomas
    Nov 2, 2011 at 17:47
1

The best practice is to put images into mdpi folder (pixel aspect 1) and then use density with all horizontal dimensions. For example:

src.left = offsetX * density; 
src.top = 0; 
src.right = (offsetX + width - 1) * density; 
src.bottom = height - 1;
dst.left = x * density; 
dst.top = y; 
dst.right = (x + width - 1) * density; 
dst.bottom = height - 1;
canvas.drawBitmap(myImageStrip, src, dst, null);

For mdpi density is 1.0, ldpi density=0.75 and for hdpi density=1.5 You can obtain these and other values using

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

There is another solution: you can put same image in all folders. The system will take proper one, so you dont need to multiply values with density. But in that case, you'll need 3 or more copies of same image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.