Android滤镜--Alpha值滤镜处理之MaskFilter

2020-07-03 11:08:29 浏览数 (1)

所谓的滤镜效果就是对图像进行一定的颜色过滤处理,颜色值是32位的int值,ARGB :A---Alpha值,RGB---颜色值
安卓中对Alpha值的滤镜处理就是利用MaskFilter,其中有两个子类继承MaskFilter
  • BlurMaskFilter--用来绘制模糊阴影
代码语言:javascript复制
    /**
     * Create a blur maskfilter.
     *
     * @param radius 阴影的半径
     * @param style  NORMOL -- 整个图像都被模糊掉
     *               SOLID -- 图像边界外产生一层与Paint颜色一致阴影效果,不影响图像的本身
     *               OUTER -- 图像边界外产生一层阴影,并且将图像变成透明效果
     *               INNER -- 在图像内部边沿产生模糊效果
     * @return       The new blur maskfilter
     */
    public BlurMaskFilter(float radius, Blur style) {
        native_instance = nativeConstructor(radius, style.native_int);
    }
代码语言:javascript复制
    @Override
    protected void onDraw(Canvas canvas) {
        RectF rectF = new RectF(50, 50, 350, 350);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);

        canvas.drawRect(rectF, paint);

        MaskFilter maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.NORMAL);
        paint.setMaskFilter(maskFilter);

        canvas.translate(400, 0);
        canvas.drawRect(rectF, paint);

        maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.SOLID);
        paint.setMaskFilter(maskFilter);

        canvas.translate(-400, 400);
        canvas.drawRect(rectF, paint);

        maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.OUTER);
        paint.setMaskFilter(maskFilter);

        canvas.translate(400, 0);
        canvas.drawRect(rectF, paint);

        maskFilter = new BlurMaskFilter(50, BlurMaskFilter.Blur.INNER);
        paint.setMaskFilter(maskFilter);

        canvas.translate(-400, 400);
        canvas.drawRect(rectF, paint);
    }
下面是普通画矩形,NORMOL ,SOLID ,OUTER ,INNER 分别的效果
  • EmbossMaskFilter--用来实现浮雕效果
代码语言:javascript复制
    /**
     * Create an emboss maskfilter
     *
     * @deprecated This subclass is not supported and should not be instantiated.
     *
     * @param direction  指定光源的位置,长度为xxx的数组标量[x,y,z]
     * @param ambient    环境光的因子 (0~1),越接近0,环境光越暗
     * @param specular   镜面反射系数 越接近0,镜面反射越强
     * @param blurRadius 模糊半径 值越大,模糊效果越明显
     * @return           the emboss maskfilter
     */
    @Deprecated
    public EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius) {
        if (direction.length < 3) {
            throw new ArrayIndexOutOfBoundsException();
        }
        native_instance = nativeConstructor(direction, ambient, specular, blurRadius);
    }
代码语言:javascript复制
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);


        Rect rectf = new Rect(10,10,300,300);
        canvas.drawRect(rectf,paint);

        canvas.translate(400,0);
        
        //第三个参数发现没什么用
        EmbossMaskFilter embossMaskFilter = new EmbossMaskFilter(new float[]{1,1,1},0.2f,1000,50);
        paint.setMaskFilter(embossMaskFilter);
        canvas.drawRect(rectf,paint);
    }
项目地址:https://gitee.com/aruba/Filter.git

0 人点赞