Skip to content Skip to sidebar Skip to footer

How To Resize Bitmaps The Optimal Way In Android?

Suppose I have a hexagon: If I resize this to use it in my application which contains a grid of hexagons: // ... bgPaint = new Paint(); bgPaint.setAntiAlias(true); bgPaint.setDith

Solution 1:

3 suggestions:

1

Try this: Turn off the system's scaling when you decode the resource by setting BitmapFactory.Options.inScaled to false:

Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg, options);

The inScaled flag should be turned off if you need a non-scaled version of the bitmap.

Then scale your bitmap with Bitmap.createScaledBitmap(...).

2

Another possible reason is that your tile's diagonal black lines contain different shades of grey:

This is a close up of your tile:

enter image description here

It's anti-aliased before it's resized. Any pixels not totally black may show up as lighter color in the resized lines. You could change your lines lines to be completely black (0xFF000000) and do anti-alias only after the resizing.

3

Another solution to this problem is to design your tile like so:

enter image description here

which avoids the problem of drawing two anti-aliased diagonal lines next to each other.


Solution 2:

Why dont use this one instead?

Bitmap.createScaledBitmap(decodedBitmap, targetWidth, targetHeight, true);

Solution 3:

You could try a hqx resizing algorithm:

Alternatively, you could paint onto a bigger surface, and scale that surface altogether.


Solution 4:

I'm resizing images as follows:

    String url = ""; //replace with path to your image  
    int imageDimension = 48; // replace with required image dimension

    //decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(url), null, o);

    //Find the correct scale value. It should be the power of 2.
    final int REQUIRED_SIZE = imageDimension;
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while(true){
        if(width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    //decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    Drawable drawable =  new BitmapDrawable(context.getResources(),    BitmapFactory.decodeStream(new FileInputStream(url), null, o2));

Post a Comment for "How To Resize Bitmaps The Optimal Way In Android?"