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先看运行效果,代码很少,这是对比效果图这里写图片描述

  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共四种方式,效果图如下。
这里写图片描述
这里写图片描述
下面是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);
“`
效果如下,图中的黑点是灯光光源位置,这个和拍照灯光师一样的打光,添加镜面反射,调节场景光线亮度。感觉用不出来啥好看的效果

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值