4

I am trying to composite a background bitmap from multiple smaller bitmaps. I am using Eclipse with the Android SDK.

I have loaded a (mostly) blank background bitmap (pixel dimensions are 320x480) like so:

Bitmap mBackground = BitmapFactory.decodeResource( res, R.drawable.background );

Then I load a bitmap 'tile' (128x128) the same way.

I have tried multiple ways of drawing the tile onto the bitmap but every single method does not work the way I would like/expect. I want to draw the tile bitmap scaled down to 64x64 and at a specific location (pixel offset) on the background bitmap.

1) Using a Canvas...

Canvas c = new Canvas( mBackground );
c.drawBitmap( mTile, null, new Rect( 10, 10, 73, 73 ), null );

This draws the tile way too big (not fullsize, not 64x64, but somewhere in between, about 66% of original)

2) Using drawables (which basically seems to be the same):

Canvas c = new Canvas( mBackground );
c.translate( 10, 10 );
d.setBounds( 0, 0, 63, 63 );
d.draw( c );

Same result as experiment #1...

The documentation mentions in numerous places (without a concise explanation anywhere) that this has to do with the device independent 'densities' thus I tried the next approach...

3) Using fixed scale resources. I created the /res/drawable-nodpi (I also tried just /res/drawable) folder and moved my resources (background and tile png images) into it. That causes an exception when creating the Canvas from the background Bitmap because an unscaled bitmap apparently becomes immutable and therefore cannot be assigned to a Canvas!

4) I tried using BitmapFactory.Options and setting inScaled to false, and passing those options to both of the decodeResource calls for the background and the tile. This has the exact same effect as #3 (exception thrown).

Nothing has worked and its becoming quite frustrating. I am sure I am just missing some detail that is peculiar to bitmaps when it comes to the different coordinate spaces but I cannot find it anywhere in the documentation or elsewhere (I've tried multiple android development sites to no avail).

Hopefully someone who has done this will see this plea! Thanks, jason

2
  • Why don't you use Bitmap.createScaledBitmap() for scaling "tile" from 128x128 to 64x64? Commented Feb 11, 2011 at 16:35
  • Basically, after trying several experiments, it doesn't appear possible to use compositing. If I load all the tiles seperately and compose the bitmap on the fly (i.e. drawing everything to a blank Canvas at once) it works just fine because the coordinate spaces all match up. Commented Feb 19, 2011 at 0:37

2 Answers 2

0

Canvas.drawBitmap() does not perform scaling afaik.

Since your tile image is originally 128x128 and you want to draw it at 64x64, the simplest trick would be to call decodeResource() with the option to reduce it in size:

BitmapFactory.Options options = new BitmapFactory.Options()
options.inSampleSize = 2; // downscale by 50%
Bitmap mTile = BitmapFactory.decodeResource( res, R.drawable.tile, options );

To do more powerful scaling (e.g. drawing your image at any size) you should wrap it in a BitmapDrawable.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't always want to scale 128 down to 64. I prolly should have picked a better example, or at least been more general :)
In which case BitmapDrawable is your friend, as per last line of answer.
The link in your answer is broken, you forgot the 'l' from .html
0

It looks like the densities of your bitmaps are different... There is a similar issue here. You should check the density of each Bitmap you load to see if it's the same problem.

1 Comment

According to the getDensity() call, they are both 160. The immutability of the unscaled Bitmap (see edits in original question for #3 & #4) seems to prevent me from being able to composite, then scale the final image.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.