Android: high quality image resizing / scaling Android: high quality image resizing / scaling android android

Android: high quality image resizing / scaling


You can use BitmapFactory.Options with BitmapFactory.decode function(s),
using inDensity and inTargetDensity

Example: you have 1600x1200 size of image and want to resize to 640x480
then 'inDensity'=5 and 'inTargetDensity'=2 (1600x2 equal to 640x5).

Hoping this help.


The top large image is simply scaled down to 450px by the layout so no artifacts.

The artifacts of the bottom large image result from scaling it down to 130px wide and then up again to about 450px by the layout. So the artifacts are made by your scaling. Try

Bitmap resized = getResizedBitmap(original, 450);

in your code and it should be fine. However, you need to adapt that to the actuall screen width of the phone either.


Briefly, good downscaling algorithm (not nearest neighbor like) consists of 2 steps:

  1. downscale using BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() as close as possible to the resolution that you need but not less than it
  2. get to the exact resolution by downscaling a little bit using Canvas::drawBitmap()

Here is detailed explanation how SonyMobile resolved this task: http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

Here is the source code of SonyMobile scale utils: http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/