您的位置:首页 > 其它

解决ImageLoader显示图片出现明显波纹的问题

2015-06-16 17:57 344 查看
在项目中经常用到开源项目imageloader,发现图片显示的时候有明显的波浪纹



单独的通过图片URL将图片保存在本地,然后直接设置到imageview里面是没有波浪纹(不使用imageloader),那么这时可以肯定是imageloader在displayimage的时候对图片做了什么处理。

通过查看显示的代码,大致如下:

DisplayImageOptions **displayOptions** = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(**Bitmap.Config.RGB_565)**.build();

imageloader.displayImage(url,imageView,**displayOptions**);


从上看到了有个 Bitmap.Config.RGB_565这个参数,这个是作为图片压缩参数;

通过查看Bitmap的内部类Config 有如下枚举变量

public static final Bitmap.Config ALPHA_8
public static final Bitmap.Config ARGB_4444
public static final Bitmap.Config ARGB_8888
public static final Bitmap.Config RGB_565


ALPHA_8就是Alpha由8位组成,代表8位Alpha位图

ARGB_4444就是由4个4位组成即16位,代表16位ARGB位图

ARGB_8888就是由4个8位组成即32位,代表32位ARGB位图

RGB_565就是R为5位,G为6位,B为5位共16位,代表16位RGB位图。

一般UI使用ps 切图都是8位的,我也问了UI 切的图片的确是8位的,不是16位的,那么压缩参数按道理应该使用Bitmap.Config.ALPHA_8的,在使用imageloader显示图片的时候发现有个规律:有的图片是正常显示的,没有波纹,有的图片有波纹,而有波纹的图片都是渐变效果跨度比较大的图片。

解决办法:

有两种方法

1. 创建DisplayImageOptions的时候压缩图像色彩模式修改成ALPHA_8

DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)     .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true)      .cacheOnDisc(true).bitmapConfig(**Bitmap.Config.ALPHA_8**).build();


保持Bitmap.Config.RGB_565不变,UI切图的时候将渐变跨度比较大的图片弄成16位

以上两种方法还是第一种方法好些,毕竟16位的图片比较大。

原图是8位,分别设置Bitmap.Config.ALPHA_8和Bitmap.Config.RGB_565效果对比





通过上面的两个图对比效果,对于某些渐变比较大的8位图片,我们压缩还是要采取ALPHA_8的。

注:

针对ImageLoader的DisplayImageOptions 我们可以提供四种压缩参数:

public static DisplayImageOptions getRGB_565_ImageOptions()
{
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();

return options;
}

public static DisplayImageOptions getALFHA_8_ImageOptions()
{
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.ALPHA_8).build();

return options;
}

public static DisplayImageOptions getARGB_4444_ImageOptions()
{
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.bitmapConfig(Bitmap.Config.ARGB_4444).build();

return options;
}

public static DisplayImageOptions getARGB_8888_ImageOptions()
{
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.ARGB_8888).build();

return options;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: