test.jsp 

 

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);

 

test.jsp
修改幾個參數的值:

 

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



首 先說明一點:在網上查到有人說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 模糊半徑。

 

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()