我成功地将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 toimageScaleType(...)
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:
- Get the actual drawn
getWidth()
andgetHeight()
of the View. If view haven’t drawn yet then go to step #2.- Get
layout_width
andlayout_height
. If both of them haven’t exact value then go to step #3.- Get
maxWidth
andmaxHeight
. If both of them are not set then go to step #4.- Get
maxImageWidth
param (maxImageWidthForMemoryCache) andmaxImageHeight
param (maxImageHeightForMemoryCache). If both of them are not set (equal 0) then go to step #5.- Get device screen dimensions.
UIL根据imageScaleType和targetSize(以及ImageView的scaleType)定义结果位图大小.