在使用Android Universal Image Loader在视图中显示图片之前,如何调整图片大小? [英] How can I resize a picture before displaying it in a view with Android Universal Image Loader?

查看:196
本文介绍了在使用Android Universal Image Loader在视图中显示图片之前,如何调整图片大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将Universal Image Loader库(1.8.3版)应用到我的应用程序,并且在尝试将图像显示在gridview项目中之前,我试图调整图像的大小(因为有时图像太大,无法将其缓存在内存中). )

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

这是我正在尝试的:

...
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倍.

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?

推荐答案

认真阅读Java文档:

Read Java docs attentively:

不会考虑传入选项的

Options.inSampleSize.图书馆根据imageScaleType(...)选项自行计算出最合适的样本量.

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),它定义了图像的目标尺寸:

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. 获取视图的实际绘制的getWidth()getHeight().如果尚未绘制视图,请转到步骤2.
  2. 获取layout_widthlayout_height.如果两个值都不完全相同,请转到步骤3.
  3. 获取maxWidthmaxHeight.如果两个都没有设置,请转到步骤4.
  4. 获取maxImageWidth参数(maxImageWidthForMemoryCache)和maxImageHeight参数(maxImageHeightForMemoryCache).如果两个都没有设置(等于0),则转到步骤#5.
  5. 获取设备屏幕尺寸.
  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根据imageScaleTypetargetSize(以及ImageView的scaleType)定义结果位图大小.

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

这篇关于在使用Android Universal Image Loader在视图中显示图片之前,如何调整图片大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆