How can I resize a picture before displaying it in a view with Android Universal Image Loader?

依然范特西╮ 提交于 2019-12-28 06:08:34

问题


I successfully applied the Universal Image Loader library (1.8.3 version) to my app, and I'm trying to resize the image before displaying it in the gridview item (because sometime image is too big to cache it 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 doesn't make image 3 times smaller for some reason.

Does someone have a better way to resize an image by exactly the specified density?


回答1:


Read Java docs attentively:

Options.inSampleSize of incoming options will NOT be considered. Library calculate the most appropriate sample size itself according to imageScaleType(...) options.

Also look into ImageSizeUtil.defineTargetSizeForView(ImageView imageView, int maxImageWidth, int maxImageHeight) which defines target size for image:

Size is defined by target view parameters, configuration parameters or device display dimensions. Size computing algorithm:

  1. Get the actual drawn getWidth() and getHeight() of the View. If view haven't drawn yet then go to step #2.
  2. Get layout_width and layout_height. If both of them haven't exact value then go to step #3.
  3. Get maxWidth and maxHeight. If both of them are not set then go to step #4.
  4. Get maxImageWidth param (maxImageWidthForMemoryCache) and maxImageHeight param (maxImageHeightForMemoryCache). If both of them are not set (equal 0) then go to step #5.
  5. Get device screen dimensions.

UIL defines result Bitmap size according to imageScaleType and targetSize (and ImageView's scaleType).




回答2:


Just try this:

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();



回答3:


You can set the imageview height and with in xml. You image will fit exactly to the mentioned height and width. Your downloaded image fits to the height and width mentioned in your xml.

You can also have a look at the issue which is similar to yours.

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



来源:https://stackoverflow.com/questions/15741947/how-can-i-resize-a-picture-before-displaying-it-in-a-view-with-android-universal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!