Android游戲開發之十六:實現圖像漸變特效
在android.graphics中提供了有關Gradient字樣的類,例如LinearGradient線性漸變、 RadialGradient徑向漸變和SweepGradient角度漸變三種,他們的基類為android.graphics.Shader。為了演 示圖像漸變效果,下面給出一個簡單的實例。
一、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軸位置,參數三和四分辨對應漸變終點,***參數為平鋪方式,這里設置為鏡像
剛才已經講到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.,所以建議使用下面的重載方法,本方法一般為NULL即可。
SweepGradient(float cx, float cy, int color0, int color1)
到此,希望大家對圖像特效處理有了一定的認識,了解這些對打好Android游戲開發的基礎很有好處。