android EmbossMaskFilter 浮雕效果實現

來源:互聯網
上載者:User

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;

效果:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.