Thursday, February 9, 2012

Android: How to transform/scale a bitmap and draw it on Canvas without losing quality

If you want to rotate a bitmap and paint it on a Canvas in an Android app, you will probably use Canvas.rotate(degrees) to rotate the canvas and then paint your bitmap using any of Canvas.drawBitmap methods. Nothing fancy but the painted image will be too pixelated - except when the rotation is in multiple of 90 degrees.

The last argument to all Canvas.drawBitmap* methods is a Paint object and most of the examples I found on internet were passing a null for it. To avoid, rather reduce, the pixelation we need to use this Paint object. Create a paint object as mentiond below and pass it as a last argument to your drawBitmap() call.
Paint paint= new Paint(Paint.FILTER_BITMAP_FLAG |
                       Paint.DITHER_FLAG |
This will significantly reduce pixelation and your app will look a lot better.

No comments: