3

I want to scale down a 500x500px resource to fit always a specific size which is determined by the width of the screen.

Currently I use the code from the Android Developers Site (Loading Large Bitmaps Efficiently), but the quality is not as good as I would use the 500x500px resource in a ImageView (as source in xml) and just scale the ImageView and not the Bitmap.

But it's slow and I want to scale the Bitmap, too, to be memory efficient and fast.

Edit: The drawable which I wanna scale is in the drawable folder of my app.

Edit2: My current approaches.

enter image description here

The left image is the method from Loading Large Bitmaps Efficiently without any modifications. The center image is done with the method provided by @Salman Zaidi with this little modification: o.inPreferredConfig = Config.ARGB_8888; and o2.inPreferredConfig = Config.ARGB_8888;

The right image is an imageview where the image source is defined in xml and the quality I wanna reach with a scaled bitmap.

Leandros
  • 16,805
  • 9
  • 69
  • 108

3 Answers3

6
private Bitmap decodeImage(File f) {
    Bitmap b = null;
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        float sc = 0.0f;
        int scale = 1;
        //if image height is greater than width
        if (o.outHeight > o.outWidth) {
            sc = o.outHeight / 400;
            scale = Math.round(sc);
        } 
        //if image width is greater than height
        else {
            sc = o.outWidth / 400;
            scale = Math.round(sc);
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}

Here '400' is the new width (in case image is in portrait mode) or new height (in case image is in landscape mode). You can set the value of your own choice.. Scaled bitmap will not take much memory space..

Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
  • Looks similar to my solution. Also I don't have a file, I have a drawable (in R.drawable.xxx). – Leandros Jan 07 '13 at 16:30
  • I had used this code to resize bitmaps.. well if you have drawable, then i suggest you should save your image as a file on sd-card, then resize it.. you can't get path of your drawable folder and items.. – Salman Zaidi Jan 07 '13 at 16:52
3

Dudes, inSampleSize param is made for memory optimization, while loading a bitmap from resources or memory. So for your issue you should use this:

Bitmap bmp = BitmapFactory.decode...;
bmp = bmp.createScaledBitmap(bmp, 400, 400, false);

inSampleSizelets lets you to scale bitmap with descret steps. Scale ratios are 2,4 and so on. So when your use decoding with options, where inSampleSize=2 you loads a 250x250 bitmap from memory and then stretch it to 400x400

Alex
  • 1,416
  • 1
  • 14
  • 24
0

Check this training:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

It shows how to resize bitmaps efficiently

noni
  • 2,927
  • 19
  • 18
  • I already use the code in the training, as written above. It's not perfect in my opinion, it's still a bit blurry. – Leandros Jan 07 '13 at 16:49
  • @Leandros I think you will find that this is a very good resource to read through. if you use the API properly you can scale the bitmap as required. And modify it to your needs (to keep max possible quality for given area) – IAmGroot Jan 07 '13 at 16:54
  • @Doomsknight I read it. Maybe I just don't get it how to achieve higher quality. Can you help me out? – Leandros Jan 07 '13 at 18:00
  • exactly, it's an example, but with a proper use you can scale the bitmaps to the size you want. I've used it to resize bitmaps to thumbnails, and it saves lots of ram – noni Jan 07 '13 at 18:00
  • Try the larger RGB type, ARGB_8888 http://developer.android.com/reference/android/graphics/Bitmap.Config.html#ARGB_8888 – noni Jan 07 '13 at 18:01