转载署源(By:KyleCe):http://blog.youkuaiyun.com/kyleceshen/article/details/50055847
最近项目不忙,本着不断改进项目的宗旨,想到了改进图片加载效果。
受启发于medium,想给图片加上加载中先显示模糊照片的效果,因缘巧合,在周刊上看到了这个开源项目
作者是扩展了ImageView做了一个BlurImageView,扒了扒源码,发现它是这么做的:
1.在控件中用picasso做了一个低质量图片的请求,然后用这个开源算法得到了一张模糊图
2.模糊图载入成功后,再去请求高质量的图片
这样一来思路就清晰了,但作者设计时并没有按照MVC的框架来做,把请求的事情做到了View层
公司的项目中,我用的是比Picasso效率更高的Glide(见博客中第三条);所以几番探索之下,发现如下可行之法:
// set default empty bitmap, or the system will use the other cached pictures instead
iv.setImageBitmap(defaultWhiteBitmap);
// define the full target, use it when the picture is already blurred
final Target fullTarget = new SimpleTarget() {
@Override
public void onResourceReady(Object resource, GlideAnimation glideAnimation) {
if (!(resource instanceof Bitmap)) return;
Bitmap fullBitmap = (Bitmap) resource;
// full image task
// do the full image task
iv.setImageBitmap(fullBitmap);
}
};
// the blur target, blur the picture
// when the blur is done, set the full target
Target blurTarget = new SimpleTarget() {
@Override
public void onResourceReady(Object resource, GlideAnimation glideAnimation) {
if (!(resource instanceof Bitmap)) return;
Bitmap bitmap = (Bitmap) resource;
// Do bitmap blur here
Bitmap blurBitmap = FastBlurUtil.doBlur(bitmap, 8, true);
iv.setImageBitmap(blurBitmap);
// full image url
String fullImageUrl = "http://www.xxxxxxxxxx";
Glide.with(FragmentMoments.this).load(fullImageUrl).asBitmap().into(fullTarget);
}
};
// blur image setting
//blur image url
String blurImageUrl = "http://www.xxxxxxxxxx";
Glide.with(FragmentMoments.this).load(blurImageUrl).asBitmap().into(blurTarget);
转载署源(By:KyleCe):http://blog.youkuaiyun.com/kyleceshen/article/details/50055847