Android 背景图流行使用高斯模糊效果,界面效果会比较好看。分析各种实现方式,实现网易云音乐图片加载高斯模糊背景。
高斯模糊实现方式
JavaBlur
NativeBlur
RenderScriptBlur
选择合适的模糊技术
性能分析直接参考Android高级模糊技术,结合实际开发情况,本文只针对NativeBlur结合Fresco图片修改BasePostprocessor实现高斯模糊。
代码实践
FastBlurPostprocessor 实现对图片的高斯模糊处理
package com.android.blur;
import android.graphics.Bitmap;
import com.enrique.stackblur.NativeBlurProcess;
import com.facebook.imagepipeline.request.BasePostprocessor;
public class FastBlurPostprocessor extends BasePostprocessor {
private float mRadius;
public FastBlurPostprocessor(float blurRadius) {
this.mRadius = blurRadius;
}
public void process(Bitmap bitmap) {
try {
bitmap.setHasAlpha(true);
NativeBlurProcess blur = new NativeBlurProcess();
blur.blur(bitmap, mRadius);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getName() {
return "FastBlurPostprocessor";
}
}
修改图片
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(SINGER_URL))
.setPostprocessor(new FastBlurPostprocessor(60f))
.build();
PipelineDraweeController controller = (PipelineDraweeController)Fresco.newDraweeControllerBuilder().setImageRequest(request)
.setOldController(mBlurView.getController())
.build();
mBlurView.setController(controller);
网友评论