Libathos
Libathos

Reputation: 3362

load Bitmap efficiently when dimension aren't known

I read through the documentation on how to load bitmaps efficiently, and I executed it perfectly for creating thumbnails. But what happens when the ImageView in which I want to load the bitmap instead of having hardcoded size (for thumbnails I used 75x75 dp) its width and height are wrap content?

For being more specific: here the documentation provides this function

decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth,
int reqHeight)

and computes the inSampleSize for the required width and height. I though of getting the device's width and height and subtract the padding of the image view but this one seems to me a bit of a hack. Is there any other way? Thank you for your time.

Upvotes: 0

Views: 56

Answers (1)

IAmGroot
IAmGroot

Reputation: 13855

Get the views width/height directly, rather than working it out from the devices width and height.

You can do this using myImageView.getHeight() and myImageView.getWidth();

http://developer.android.com/reference/android/view/View.html

[Width and Height]. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

Then using the actual height/width, you can get the bitmap efficiently for the exact resolution needed.

Upvotes: 1

Related Questions