1

Since that loading large bitmaps to show on a device screen in android(the correct way) is not a trivial task, I took a look on some tutorials on how to effectively make it. I'm already aware that you need to do the following in order to make a memory efficient image loader method:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //do that to avoid loading all the information on the heap to decode
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);

Acoording to the google tutorial you should make a sampled sized image, until this point I understand, you should make a method like this:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

And Finally you call the method like this:

imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), drawable.big_image, 200, 200));

And here is where my doubt lies: How to know the best size values according to the devices's screen size/resolution ? Is there any method that I can embed on my code which returns the optimal screen resolution to load an image without the pixelated effect and yet not too big that would blow up the VM heap ? This is one of the biggest challenges I'm facing on my project right now. I searched this link(and others I don't remember) but I couldn't find the answer I'm looking for.

Community
  • 1
  • 1
  • 1
    "How to know the best size values according to the devices's screen size/resolution ?" -- the best size is the size of your `ImageView` in pixels, usually. Going higher than that is a waste of heap space. Going smaller than that is possible, but that will give you the "pixelated effect". Since `inSampleSize` is limited to powers of 2, your actual size will vary somewhat from the size of the `ImageView` anyway. The size of your `ImageView` is a design decision. – CommonsWare Apr 05 '15 at 20:39

1 Answers1

1

As a suggestion ,

  1. You can define the approx : width and height of your imageview as a portion of device width and height in pixels. As ex:

    imageview width = 0.8 of device width  & imageview height = 0.4 of device height
    
  2. Then you can calculate the device width and height in pixels. There by you can get the actual required imageview size according to relevant portion.

  3. Then you can pass the calculated imageview width and height in to "decodeSampledBitmapFromResource" method.

Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
  • Looks like a decent way to do it. Although I believe there are other ways to accomplish that. Nevertheless I'll try to do what you're suggesting and see if I can get acceptable results. Thank you very much for your answer, I just don't upvote because I don't have enough reputation. – lcastro.oliveira Apr 06 '15 at 21:24