圖像漸變特效

http://www.android123.com.cn/androidkaifa/565.html

Android遊戲開發之旅17 圖像漸變特效

   在Android遊戲開發中我們不免要涉及到一些圖形特效處理,今天主要看下Android平臺下實現漸變效果。在android.graphics中我們可以找到有關Gradient字樣的類,比如LinearGradient 線性漸變、RadialGradient徑向漸變和 角度漸變SweepGradient 三種,他們的基類爲android.graphics.Shader。爲了顯示出效果android123使用一個簡單的例子來說明。

  一、LinearGradient線性漸變

  在android平臺中提供了兩種重載方式來實例化該類分別爲,他們的不同之處爲參數中第一種方法可以用顏色數組,和位置來實現更細膩的過渡效果,比如顏色採樣int[] colors數組中存放20種顏色,則漸變將會逐一處理。而第二種方法參數僅爲起初顏色color0和最終顏色color1。

LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile) 
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile) 
 
  使用實例如下

  Paint p=new Paint();

  LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);  //參數一爲漸變起初點座標x位置,參數二爲y軸位置,參數三和四分辨對應漸變終點,最後參數爲平鋪方式,這裏設置爲鏡像

  剛纔Android開發網已經講到Gradient是基於Shader類,所以我們通過Paint的setShader方法來設置這個漸變,代碼如下:

  p.setShader(lg);

  canvas.drawCicle(0,0,200,p); //參數3爲畫圓的半徑,類型爲float型。

  二、 RadialGradient鏡像漸變

  有了上面的基礎,我們一起來了解下徑向漸變。和上面參數唯一不同的是,徑向漸變第三個參數是半徑,其他的和線性漸變相同。

  RadialGradient(float x, float y, float radius, int[] colors, float[] positions, Shader.TileMode tile) 
  RadialGradient(float x, float y, float radius, int color0, int color1, Shader.TileMode tile)

 三、 SweepGradient角度漸變

  對於一些3D立體效果的漸變可以嘗試用角度漸變來完成一個圓錐形,相對來說比上面更簡單,前兩個參數爲中心點,然後通過載入的顏色來平均的漸變渲染。

SweepGradient(float cx, float cy, int[] colors, float[] positions)  //對於最後一個參數SDK上的描述爲May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.,所以Android123建議使用下面的重載方法,本方法一般爲NULL即可。
SweepGradient(float cx, float cy, int color0, int color1) 

  有關Android遊戲開發中需要遇到的圖形繪製技術,我們近期仍然需要花費更多的篇幅去介紹,希望大家有更堅實的基礎,有關更多的Android開發內容請關注Android123. 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章