在Android游戲開發中我們不免要涉及到一些圖形特效處理,今天主要看下Android平臺下實現漸變效果。在 android.graphics中我們可以找到有關Gradient字樣的類,比如LinearGradient 線性漸變、RadialGradient徑向漸變和 角度漸變SweepGradient 三種,他們的基類為android.graphics.Shader。為了顯示出效果android123使用一個簡單的例子來說明。
一、LinearGradient線性漸變
在android平臺中提供了兩種重載方式來實例化該類分別為,他們的不同之處為參數中第一種方法可以用顏色數組,和位置來實現更細膩的過渡效果,比如顏色采樣int[] colors數組中存放20種顏色,則漸變將會逐一處理。而第二種方法參數僅為起初顏色color0和最終顏色color1。
view plaincopy to clipboardprint?
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)
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)
使用實例如下
view plaincopy to clipboardprint?
Paint p=new Paint();
LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR); //參數一為漸變起初點坐標x位置,參數二為y軸位置,參數三和四分辨對應漸變終點,最后參數為平鋪方式,這里設置為鏡像
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方法來設置這個漸變,代碼如下:
view plaincopy to clipboardprint?
p.setShader(lg);
canvas.drawCicle(0,0,200,p); //參數3為畫圓的半徑,類型為float型。
p.setShader(lg);
canvas.drawCicle(0,0,200,p); //參數3為畫圓的半徑,類型為float型。
二、 RadialGradient鏡像漸變
有了上面的基礎,我們一起來了解下徑向漸變。和上面參數唯一不同的是,徑向漸變第三個參數是半徑,其他的和線性漸變相同。
view plaincopy to clipboardprint?
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)
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立體效果的漸變可以嘗試用角度漸變來完成一個圓錐形,相對來說比上面更簡單,前兩個參數為中心點,然后通過載入的顏色來平均的漸變渲染。
view plaincopy to clipboardprint?
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)
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/JavaTiger427/archive/2010/11/25/6034673.aspx
--
學海無涯