Saturday, 17 August 2013

Android: Bitmap appears smaller after setting with setBackgroundDrawable

Android: Bitmap appears smaller after setting with setBackgroundDrawable

I have a weird problem where using a combination of drawBitmap and
setBackgroundDrawable makes my image appear 2x smaller than it actually
is.
Below is my code that creates a new bitmap and canvas. It then draws the
bitmap on the canvas and puts the new bitmap as the background of an
existing layout. I must use this method because I am going to manipulate
the canvas later on.
Bitmap left = BitmapFactory.decodeResource(getResources(), resID);
// Create a temporary bitmap
Bitmap tempBitmap = Bitmap.createBitmap(
left.getWidth(),
left.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(tempBitmap);
//Draw bitmap on canvas
tempCanvas.drawBitmap(left, 0, 0, null);
background.setBackgroundDrawable(tempBitmap);
I tried using tempCanvas.scale(2, 2); and this does scale the "pixels"
back to normal but it doesn't scale the rectangle.
If I use background.setBackgroundResource(resID); the background image
appears at the correct size, which rules out any problems with my
MDPI/HDPI/XHDPI folders.
Why is my background image appearing at a smaller scale when setting it
with setBackgroundDrawable()?

No comments:

Post a Comment