天天看點

android 漸變

一、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軸位置,參數三和四分辨對應漸變終點,最後參數為平鋪方式,這裡設定為鏡像.

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)