How to resize an image before displaying it in a view with Android Universal Image Loader?

I have successfully applied the Universal Image Loader library (version 1.8.3) to my application, and I am trying to resize the image before displaying it in the gridview element (because sometimes the image is too large to cache in memory.)

Here is what I am trying:

... BitmapFactory.Options resizeOptions = new BitmapFactory.Options(); resizeOptions.inSampleSize = 3; // decrease size 3 times resizeOptions.inScaled = true; options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.blank) .showImageForEmptyUri(R.drawable.no_image) .cacheInMemory() .cacheOnDisc() .decodingOptions(resizeOptions) .build(); ... 

This code does not make the image 3 times smaller for any reason.

Does anyone have a better way to resize an image with exactly the specified density?

+7
source share
3 answers

Read the Java docs carefully:

Options.inSampleSize incoming parameters will NOT be considered. The library calculates the most suitable sample size according to the parameters of imageScaleType(...) .

Also look at ImageSizeUtil.defineTargetSizeForView(ImageView imageView, int maxImageWidth, int maxImageHeight) , which determines the size of the target for the image:

The size is determined by the parameters of the target view, configuration parameters, or the size of the device display. Size Algorithm:

  • Get the actual drawings getWidth() and getHeight() in the view. If the view has not yet been drawn, go to step # 2.
  • Get layout_width and layout_height . If both of them do not have an exact value, go to step # 3.
  • Get maxWidth and maxHeight . If both are not installed, go to step 4.
  • Get maxImageWidth param (maxImageWidthForMemoryCache) and maxImageHeight param (maxImageHeightForMemoryCache). If both of them are not installed (equal to 0), go to step 5.
  • Get device screen sizes.

UIL determines the result The size of the bitmap according to imageScaleType and targetSize (and ImageView scaleType).

+22
source

Just try the following:

 options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.blank) .showImageForEmptyUri(R.drawable.no_image) .cacheInMemory() .cacheOnDisc() .decodingOptions(resizeOptions) .postProcessor(new BitmapProcessor() { @Override public Bitmap process(Bitmap bmp) { return Bitmap.createScaledBitmap(bmp, 300, 300, false); } }) .build(); 
+14
source

You can set the height of the image with xml. The image will fit exactly the specified height and width. The uploaded image matches the height and width specified in your xml.

You can also look at a problem that is similar to yours.

https://github.com/nostra13/Android-Universal-Image-Loader/issues/183

+2
source

All Articles