MaskFilter(滤镜)BlurMaskFilter(高斯滤镜)和EmbossMaskFilter(浮雕滤镜)的简单介绍和使用

*官方介绍:***MaskFilter is the base class for object that perform transformations on an alpha-channel mask before drawing it. A subclass of MaskFilter may be installed into a Paint. Blur and emboss are implemented as subclasses of MaskFilter.
字面意思:滤镜是再在绘制之前改变透明度。有两个子类

BlurMaskFilter(高斯滤镜)This takes a mask, and blurs its edge by the specified radius. Whether or or not to include the original mask, and whether the blur goes outside, inside, or straddles, the original mask’s border, is controlled by the Blur enum.
意思指定半径和模式改变透明度。
EmbossMaskFilter(浮雕滤镜),官网上没有任何的介绍。

先看BlurMaskFilter透明遮罩的效果,任何api先看运行效果,代码很少,这是对比效果图MaskFilter(滤镜)BlurMaskFilter(高斯滤镜)和EmbossMaskFilter(浮雕滤镜)的简单介绍和使用_第1张图片

  public FiterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);
        bmWidth=bitmap.getWidth();
        bmHeigth=bitmap.getHeight();
        Log.i(TAG, "FiterView: 图片高度"+bmHeigth);
        /**
         * @param radius 半径
         * @param style  高斯遮罩
         * @return       The new blur maskfilter
         */
        filter = new BlurMaskFilter(RADIUS, BlurMaskFilter.Blur.NORMAL);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setMaskFilter(filter);
        //正常的paint
        paintNormal = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //注意:绘制的时候要关闭硬件加速,否则没有任何效果
        this.setLayerType(LAYER_TYPE_SOFTWARE,null);
        canvas.drawBitmap(bitmap,120,0,paintNormal);
        canvas.drawBitmap(bitmap,120,80+bmHeigth,paint);
    }

注意:一定要关闭硬件加速,硬件有很多api不支持,不支持的api就绘制不出来。

//高斯滤镜总用有NORMAL、SOLID、OUTER、INNER共四种方式,效果图如下。
MaskFilter(滤镜)BlurMaskFilter(高斯滤镜)和EmbossMaskFilter(浮雕滤镜)的简单介绍和使用_第2张图片
MaskFilter(滤镜)BlurMaskFilter(高斯滤镜)和EmbossMaskFilter(浮雕滤镜)的简单介绍和使用_第3张图片
下面是EmbossMaskFilter浮雕滤镜简介和使用

/**
* Create an emboss maskfilter
*
* @param direction float[] direction 三维坐标点,指定打灯(灯光)的位置
* @param ambient float 周围光或者背景光强度(0到1)
* @param specular float 镜面高光系数(e.g. 8)
* @param blurRadius float 模糊半径 (e.g. 3)
* @return the emboss maskfilter
*/
float[] direction=new float[]{60f,200f,20f};
EmbossMaskFilter emboss=new EmbossMaskFilter(direction,0.4f,5f,10f);
“`
效果如下,图中的黑点是灯光光源位置,这个和拍照灯光师一样的打光,添加镜面反射,调节场景光线亮度。感觉用不出来啥好看的效果

MaskFilter(滤镜)BlurMaskFilter(高斯滤镜)和EmbossMaskFilter(浮雕滤镜)的简单介绍和使用_第4张图片

你可能感兴趣的:(Android)