Skip to content
Advertisement

Draw a scaled Bitmap to the Canvas?

The following code defines my Bitmap:

JavaScript

… And the following code is used to execute/draw it (the unscaled Bitmap):

JavaScript

My question is, how might I set it to draw the scaled Bitmap returned in the form of Bitmap scaled, and not the original?

Advertisement

Answer

Define a new class member variable: Bitmap mScaledBackground; Then, assign your newly created scaled bitmap to it: mScaledBackground = scaled; Then, call in your draw method: canvas.drawBitmap(mScaledBackground, 0, 0, null);

Note that it is not a good idea to hard-code screen size in the way you did in your snippet above. Better would be to fetch your device screen size in the following way:

JavaScript

And it would be probably better not to declare a new bitmap for the only purpose of drawing your original background in a scaled way. Bitmaps consume a lot of precious resources, and usually a phone is limited to a few MB of Bitmaps you can load before your app ungracefully fails. Instead you could do something like this:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement