项目需要做一个半透明的高斯模糊,用来覆盖下面的布局,样式类似下面这种

所以第一时间想到了高斯模糊来做。
半透明的高斯模糊其实不难实现,只需要知道android.support.v8.renderscript这个包基本上就可以实现了。
使用之前可以先看看 https://www.jianshu.com/p/dd0a339e288e
public class BlurringView extends android.support.v7.widget.AppCompatImageView {
public BlurringView(Context context) {
this(context, null);
}
public BlurringView(Context context, AttributeSet attrs) {
super(context, attrs);
final Resources res = getResources();
//模糊半径 值越大,性能要求越高,模糊半径不能超过25
final int defaultBlurRadius = res.getInteger(R.integer.default_blur_radius);
//采集样式
final int defaultDownsampleFactor = res.getInteger(R.integer.default_downsample_factor);
//覆盖颜色
final int defaultOverlayColor = res.getColor(R.color.default_overlay_color);
//初始化RenderScript
initializeRenderScript(context);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PxBlurringView);
setBlurRadius(a.getInt(R.styleable.PxBlurringView_blurRadius, defaultBlurRadius));
setDownsampleFactor(a.getInt(R.styleable.PxBlurringView_downsampleFactor,
defaultDownsampleFactor));
setOverlayColor(a.getColor(R.styleable.PxBlurringView_overlayColor, defaultOverlayColor));
a.recycle();
}
public void setBlurredView(View blurredView) {
mBlurredView = blurredView;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBlurredView != null) {
if (prepare()) {
// If the background of the blurred view is a color drawable, we use it to clear
// the blurring canvas, which ensures that edges of the child views are blurred
// as well; otherwise we clear the blurring canvas with a transparent color.
if (mBlurredView.getBackground() != null && mBlurredView.getBackground() instanceof ColorDrawable) {
mBitmapToBlur.eraseColor(((ColorDrawable) mBlurredView.getBackground()).getColor());
} else {
mBitmapToBlur.eraseColor(Color.TRANSPARENT);
}
//将需要被模糊的布局画成bitmap
mBlurredView.draw(mBlurringCanvas);
blur();
canvas.save();
canvas.translate(mBlurredView.getX() - getX(), mBlurredView.getY() - getY());
canvas.scale(mDownsampleFactor, mDownsampleFactor);
canvas.drawBitmap(mBlurredBitmap, 0, 0, null);
canvas.restore();
}
canvas.drawColor(mOverlayColor);
}
}
public void setBlurRadius(int radius) {
mBlurScript.setRadius(radius);
}
public void setDownsampleFactor(int factor) {
if (factor <= 0) {
throw new IllegalArgumentException("Downsample factor must be greater than 0.");
}
if (mDownsampleFactor != factor) {
mDownsampleFactor = factor;
mDownsampleFactorChanged = true;
}
}
public void setOverlayColor(int color) {
mOverlayColor = color;
}
private void initializeRenderScript(Context context) {
mRenderScript = RenderScript.create(context);
//映射脚本内核,
mBlurScript = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
}
protected boolean prepare() {
final int width = mBlurredView.getWidth();
final int height = mBlurredView.getHeight();
if (mBlurringCanvas == null || mDownsampleFactorChanged
|| mBlurredViewWidth != width || mBlurredViewHeight != height) {
mDownsampleFactorChanged = false;
mBlurredViewWidth = width;
mBlurredViewHeight = height;
int scaledWidth = width / mDownsampleFactor;
int scaledHeight = height / mDownsampleFactor;
// The following manipulation is to avoid some RenderScript artifacts at the edge.
scaledWidth = scaledWidth - scaledWidth % 4 + 4;
scaledHeight = scaledHeight - scaledHeight % 4 + 4;
if (mBlurredBitmap == null
|| mBlurredBitmap.getWidth() != scaledWidth
|| mBlurredBitmap.getHeight() != scaledHeight) {
//创建模糊的蒙板
mBitmapToBlur = Bitmap.createBitmap(scaledWidth, scaledHeight,
Bitmap.Config.ARGB_8888);
if (mBitmapToBlur == null) {
return false;
}
mBlurredBitmap = Bitmap.createBitmap(scaledWidth, scaledHeight,
Bitmap.Config.ARGB_8888);
if (mBlurredBitmap == null) {
return false;
}
}
mBlurringCanvas = new Canvas(mBitmapToBlur);
mBlurringCanvas.scale(1f / mDownsampleFactor, 1f / mDownsampleFactor);
mBlurInput = Allocation.createFromBitmap(mRenderScript, mBitmapToBlur,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mBlurOutput = Allocation.createTyped(mRenderScript, mBlurInput.getType());
}
return true;
}
/**
* 制作模糊样式
*/
protected void blur() {
mBlurInput.copyFrom(mBitmapToBlur);
mBlurScript.setInput(mBlurInput);
mBlurScript.forEach(mBlurOutput);
mBlurOutput.copyTo(mBlurredBitmap);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mRenderScript != null) {
mRenderScript.destroy();
}
}
private int mDownsampleFactor;
private int mOverlayColor;
private View mBlurredView;
private int mBlurredViewWidth, mBlurredViewHeight;
private boolean mDownsampleFactorChanged;
private Bitmap mBitmapToBlur, mBlurredBitmap;
private Canvas mBlurringCanvas;
private RenderScript mRenderScript;
private ScriptIntrinsicBlur mBlurScript;
private Allocation mBlurInput, mBlurOutput;
}
一些属性的代码就不贴了。
就是两个Bitmap,将一层半透明的覆盖在上面就行了。这里有两个小问题
1.在低版本和某些特定的机型里面是没有RenderScript以及相关的so文件的。所以这里我们需要将相应的so文件导入

以及设置支持的SO库架构

- 被模糊的布局里面有Glide加载图片的时候,图片并不能正确加载。
原因是在异步操作的时候,Glide图片还未加载完成的时候,图片模糊已经完成了,导致看不到布局里面的图片完成样式。所以需要先完成Glide的图片加载。
RoundedCornersTransform transform3 = new RoundedCornersTransform(itemView.getContext(), DensityUtil.dip2px(6));
transform3.setNeedCorner(true, true, true, true);
RequestOptions options = new RequestOptions().transform(transform3).placeholder(R.drawable.img_bitmap_holder_gray);
Glide.with(itemView.getContext())
.asBitmap()
.load(R.drawable.bg_opacity_mask)
.apply(options)
.listener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
binding.bvGaussian.setBlurRadius(2);
binding.bvGaussian.setBlurredView(binding.llContent);
binding.bvGaussian.invalidate();
binding.tvRewardGuass.setText(bean.getBuyerCommision());
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
binding.bvGaussian.setBlurRadius(2);
binding.bvGaussian.setBlurredView(binding.llContent);
binding.bvGaussian.invalidate();
binding.tvRewardGuass.setText(bean.getBuyerCommision());
return false;
}
})
.into(binding.ivGaussian);
网友评论