如何在使用Android Universal Image Loader的视图中显示之前调整图片大小?

前端之家收集整理的这篇文章主要介绍了如何在使用Android Universal Image Loader的视图中显示之前调整图片大小?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@我成功地将Universal Image Loader库(1.8.3版本)应用到我的应用程序中,并且我试图在将其显示在gridview项目之前调整图像大小(因为有时图像太大而无法将其缓存在内存中.)

这是我正在尝试的:

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

由于某种原因,此代码不会使图像缩小3倍.

有人有更好的方法来按照指定的密度调整图像大小吗?

解决方法

认真阅读Java文档:

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

另请查看定义图像目标大小的ImageSizeUtil.defineTargetSizeForView(ImageView imageView,int maxImageWidth,int maxImageHeight):

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根据imageScaleType和targetSize(以及ImageView的scaleType)定义结果位图大小.

原文链接:https://www.f2er.com/android/317992.html

猜你在找的Android相关文章