android EmbossMaskFilter 浮雕效果實現
網上貌似還沒有EmbossMaskFilter的相關文章,所以我在這裡對他進行研究。
首先說明一點:在網上查到有人說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;
效果: