Effdee
Effdee

Reputation: 53

Canvas Bitmap drawing and Screen-Density

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

Upvotes: 1

Views: 7970

Answers (2)

Elvis Popovic
Elvis Popovic

Reputation: 11

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.

Upvotes: 1

Thomas
Thomas

Reputation: 1508

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

Upvotes: 1

Related Questions