Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bitmap + canvas density dependency

Tags:

android

I just can't figure out how to draw on a bitmap (with the help of a canvas) so that the result won't be device density dependent.

Here's the code that makes the drawing:

ImageView iv = (ImageView)findViewById(R.id.container);
Bitmap result = BitmapFactory.decodeResource(getResources(), R.drawable.big_picture).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.small_picture), actualX, actualY, null);
iv.setImageBitmap(result);

I load a big_picture in the Bitmap result and i want to draw a small_picture on that but at a specified position. If i set actualX and actualY it's ok on a device with the same density. However devices with different densities "scale" the canvas.

The interesting part is that only the small_picture is got "scaled" and goes out of the screen but the pig_picture behind it just fits the screen well on any density screen. Why is that? How can i draw the small_picture on the big_picture densitiy independently? Thank you!

like image 865
IBoS Avatar asked Dec 07 '25 09:12

IBoS


1 Answers

I've found out what's the case after debugging the Bitmap. Unless you're putting images to drawable-nodpi the image will be re-sized to match the nominal device density. (Which are 120, 160, 240, 320) If you "load" an image to a Bitmap and Canvas they will have one of these densities regardless of the original image's.

So one solution is to put the images to drawable-nodpi. After that they will behave the same on all densities.

Another solution is to multiply the coordinates based on the ratios of the densities above.

...or you can just make custom images and coordinates for all qualifiers.

like image 149
IBoS Avatar answered Dec 08 '25 22:12

IBoS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!