android EmbossMaskFilter 浮雕效果实现

网上貌似还没有EmbossMaskFilter的相关文章,所以我在这里对他进行研究。

Lee出品,转载请注明出处。

首先说明一点:在网上查到有人说MaskFilter不能使用,也就是有的效果不能显示,在此予以纠正:由于android 4.0以上系统会默认开启硬件加速(可以通过代码canvas.isHardwareAccelerated()来判断),由于硬件加速不够完善,导致部分效果不能正常显示,如何关闭硬件加速戳:点击打开链接

好了,回到正题,先来看看源码:

public class EmbossMaskFilter extends MaskFilter {
    /**
     * Create an emboss maskfilter
     *
     * @param direction  array of 3 scalars [x, y, z] specifying the direction of the light source
     * @param ambient    0...1 amount of ambient light
     * @param specular   coefficient for specular highlights (e.g. 8)
     * @param blurRadius amount to blur before applying lighting (e.g. 3)
     * @return           the emboss maskfilter
     */
    public EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius) {
        if (direction.length < 3) {
            throw new ArrayIndexOutOfBoundsException();
        }
        native_instance = nativeConstructor(direction, ambient, specular, blurRadius);
    }

    private static native int nativeConstructor(float[] direction, float ambient, float specular, float blurRadius);
}

源码其实也是很简单的,创建一个浮雕滤镜,也就是指定光源的方向和环境光强度来添加浮雕效果。

看看参数含义:

direction 是float数组,定义长度为3的数组标量[x,y,z],来指定光源的方向

ambient 取值在0到1之间,定义背景光 或者说是周围光

specular 定义镜面反射系数。

blurRadius 模糊半径。

如下代码设置的效果

		float []direction = new float[]{10, 10, 10};
		float ambient = 0.5f;
		float specular = 1;
		float blurRadius = 1;
		EmbossMaskFilter filter = new EmbossMaskFilter(direction, ambient, specular, blurRadius);
		paint.setMaskFilter(filter);
效果:

修改几个参数的值:

		float ambient = 0.1f;
		float specular = 5;
		float blurRadius = 5;

效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值