Android游戲開發(fā)之十六:實(shí)現(xiàn)圖像漸變特效
在android.graphics中提供了有關(guān)Gradient字樣的類,例如LinearGradient線性漸變、 RadialGradient徑向漸變和SweepGradient角度漸變?nèi)N,他們的基類為android.graphics.Shader。為了演 示圖像漸變效果,下面給出一個(gè)簡(jiǎn)單的實(shí)例。
一、LinearGradient線性漸變
在android平臺(tái)中提供了兩種重載方式來(lái)實(shí)例化該類分別為,他們的不同之處為參數(shù)中***種方法可以用顏色數(shù)組,和位置來(lái)實(shí)現(xiàn)更細(xì)膩的過(guò)渡效果,比如顏 色采樣int[] colors數(shù)組中存放20種顏色,則漸變將會(huì)逐一處理。而第二種方法參數(shù)僅為起初顏色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)
使用實(shí)例如下:
- Paint p=new Paint();
- LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR); //參數(shù)一為漸變起初點(diǎn)坐標(biāo)x位置,參數(shù)二為y軸位置,參數(shù)三和四分辨對(duì)應(yīng)漸變終點(diǎn),***參數(shù)為平鋪方式,這里設(shè)置為鏡像
剛才已經(jīng)講到Gradient是基于Shader類,所以我們通過(guò)Paint的setShader方法來(lái)設(shè)置這個(gè)漸變,代碼如下:
- p.setShader(lg);
- canvas.drawCicle(0,0,200,p); //參數(shù)3為畫圓的半徑,類型為float型。
二、RadialGradient鏡像漸變
有了上面的基礎(chǔ),我們一起來(lái)了解下徑向漸變。和上面參數(shù)唯一不同的是,徑向漸變第三個(gè)參數(shù)是半徑,其他的和線性漸變相同。
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角度漸變
對(duì)于一些3D立體效果的漸變可以嘗試用角度漸變來(lái)完成一個(gè)圓錐形,相對(duì)來(lái)說(shuō)比上面更簡(jiǎn)單,前兩個(gè)參數(shù)為中心點(diǎn),然后通過(guò)載入的顏色來(lái)平均的漸變渲染。
SweepGradient(float cx, float cy, int[] colors, float[] positions) //對(duì)于***一個(gè)參數(shù)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)
到此,希望大家對(duì)圖像特效處理有了一定的認(rèn)識(shí),了解這些對(duì)打好Android游戲開發(fā)的基礎(chǔ)很有好處。