Android ImageView蒙版、高斯模糊


关于ImageView蒙版、高斯模糊的实现方法,蒙版实现原理很简单,就是继承自ImageView绘制透明涂层,高斯模糊,可以直接使用Glide中apply() 方法实现!这两个功能我封装到了一起!如下!


效果图

在这里插入图片描述


自定义View

package com.chengyinan.tests;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;

import java.security.MessageDigest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 自定义MaskImageView
 *
 * @author ThirdGoddess
 * @email ofmyhub@gmail.com
 * @Github https://github.com/ThirdGoddess
 * @date :2020-05-23 21:54
 */
@SuppressLint("AppCompatCustomView")
public class MaskImageView extends ImageView {
    private Context context;

    //蒙版色值
    private int color = Color.parseColor("#81FFFFFF");

    //是否显示蒙版
    private boolean isShowMask = false;

    public MaskImageView(Context context) {
        super(context);
        this.context = context;
    }

    public MaskImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public MaskImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

   
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isShowMask) {
            canvas.drawColor(color);
        }
    }

    /**
     * 设置蒙版颜色,可以通过类似于(#81FFFFFF)来控制透明度
     */
    public MaskImageView setMaskColor(int color) {
        this.color = color;
        return this;
    }

    /**
     * 显示蒙版
     */
    public void showMask() {
        isShowMask = true;
        invalidate();
    }

    /**
     * 关闭蒙版
     */
    public void dismissMask() {
        isShowMask = false;
        invalidate();
    }

    /**
     * 高斯模糊处理
     *
     * @param radius
     * @return
     */
    public Bitmap setGaussblur(int radius, Bitmap source) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            RenderScript renderScript = RenderScript.create(context);
            final Allocation input = Allocation.createFromBitmap(renderScript, source);
            final Allocation output = Allocation.createTyped(renderScript, input.getType());
            ScriptIntrinsicBlur scriptIntrinsicBlur;
            scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
            scriptIntrinsicBlur.setInput(input);
            scriptIntrinsicBlur.setRadius(radius);
            scriptIntrinsicBlur.forEach(output);
            output.copyTo(source);
            renderScript.destroy();
            return source;
        } else {
            return source;
        }
    }

    /**
     * 开启高斯模糊
     *
     * @return
     */
    public RequestOptions setGaussBlur() {
        return RequestOptions.bitmapTransform(new GlideBlurTransformation(context));
    }

    /**
     * 压缩Bitmap
     */
    public class GlideBlurTransformation extends CenterCrop {
        private Context context;

        GlideBlurTransformation(Context context) {
            this.context = context;
        }

        @Override
        protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap bitmap = super.transform(pool, toTransform, outWidth, outHeight);
            return blurBitmap(context, bitmap, 25, (int) (outWidth * 0.2), (int) (outHeight * 0.2));
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        }
    }

    /**
     * 模糊Bitmap处理
     *
     * @param context
     * @param source
     * @param blurRadius
     * @param outWidth
     * @param outHeight
     * @return
     */
    public Bitmap blurBitmap(Context context, Bitmap source, float blurRadius, int outWidth, int outHeight) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Bitmap inputBitmap = Bitmap.createScaledBitmap(source, outWidth, outHeight, false);
            Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
            RenderScript rs = RenderScript.create(context);
            ScriptIntrinsicBlur blurScript;
            blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
            Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
            Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
            blurScript.setRadius(blurRadius);
            blurScript.setInput(tmpIn);
            blurScript.forEach(tmpOut);
            tmpOut.copyTo(outputBitmap);
            return outputBitmap;
        } else {
            return source;
        }
    }
}

使用方法


设置蒙版颜色

imageView.setMaskColor(int color);//设置蒙版颜色

显示蒙版(可动态控制)

imageView.showMask();

隐藏蒙版(可动态控制)

imageView.dismissMask();

高斯模糊实现

Glide.with(Context).load(imgUrl)
	.apply(imageView.setGaussBlur())//这是重点
	.into(imageView);
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值