Reputation: 32233
I want to paint a Bitmap scaled in a canvas (based on the Bitmap and the View size). The problem is that if I scale the canvas before painting the bitmap:
canvas.scale(1.5f,1.5f);
canvas.drawBitmap(mBitmap, 0, 0, paint);
This is the result:
But if instead of scaling the canvas I scale the bitmap:
Bitmap scaled = Bitmap.createScaledBitmap(
mBitmap, bitmapWidth, bitmapHeight,
true);
canvas.drawBitmap(scaledBitmap, 0, 0, paint);
The result is much better:
The problem is that the Bitmap is pretty big and having in memory both the original and the scaled bitmap may be problematic.
Is there any way to obtain the second result without creating a scaled Bitmap every time the Bitmap or the View bounds changes?
Upvotes: 4
Views: 1466
Reputation: 32233
The solution to my problem was that the Paint hasn't enabled the FILTER_BITMAP_FLAG flag.
Upvotes: 1
Reputation: 13855
Use inSampleSize
in BitmapOptions
when reading the bitmap from your source to scale it accordingly (it is passed as an extra parameter).
This is androids preferred way of loading images in nearly all cases to avoid memory overflow, as the full image is Never loaded into memory.
The android docs on Loading BItmaps efficiently can be found here.
With example code on how to retrieve an image of desired size from your source. (resource, file, etc etc)
The two methods in Loading Large Bitmaps Efficiently
are the ones you are looking for.
Upvotes: 1
Reputation: 3163
Use a matrix that contains the scaling info when drawing to canvas
canvas.drawBitmap(x,y,..., matrix...)
Upvotes: 1