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

我成功地将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)定义结果位图大小.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...