Android 网易云音乐图片高斯模糊

Android 背景图流行使用高斯模糊效果,界面效果会比较好看。分析各种实现方式,实现网易云音乐图片加载高斯模糊背景。

注意:请更新 fresco 版本,内部已实现 BlurPostProcessor

使用方式如下:

ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(SINGER_URL))
        .setPostprocessor(new BlurPostProcessor(10, this, 1))
        .build();

PipelineDraweeController controller = (PipelineDraweeController)Fresco.newDraweeControllerBuilder()
        .setImageRequest(request)
        .setOldController(mBlurView.getController())
        .build();

mBlurView.setController(controller);

源码:android-fresco-blur


高斯模糊实现方式

  • 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);

推荐阅读更多精彩内容