美文网首页
Android滤镜--Alpha值滤镜处理之MaskFilter

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

作者: aruba | 来源:发表于2020-01-10 09:59 被阅读0次

    所谓的滤镜效果就是对图像进行一定的颜色过滤处理,颜色值是32位的int值,ARGB :A---Alpha值,RGB---颜色值

    安卓中对Alpha值的滤镜处理就是利用MaskFilter,其中有两个子类继承MaskFilter
    • BlurMaskFilter--用来绘制模糊阴影
        /**
         * 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);
        }
    
        @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 分别的效果
    maskfilter.jpg
    • EmbossMaskFilter--用来实现浮雕效果
        /**
         * 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);
        }
    
        @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);
        }
    
    embossMaskFilter.jpg

    项目地址:https://gitee.com/aruba/Filter.git

    相关文章

      网友评论

          本文标题:Android滤镜--Alpha值滤镜处理之MaskFilter

          本文链接:https://www.haomeiwen.com/subject/dhkpactx.html