Android繪圖之SweepGradient(10)
阿新 • • 發佈:2019-01-02
SweepGradient掃描漸變
SweepGradient可以實現掃描漸變渲染,類似雷達掃描圖,漸變圓弧,漸變進度條等,建構函式有兩個:
/**
* A Shader that draws a sweep gradient around a center point.
*
* @param cx The x-coordinate of the center
* @param cy The y-coordinate of the center
* @param colors The colors to be distributed between around the center.
* There must be at least 2 colors in the array.
* @param positions 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.
*/
public SweepGradient(float cx, float cy,
@NonNull @ColorInt int colors[], @Nullable float positions[]);
/**
* A Shader that draws a sweep gradient around a center point.
*
* @param cx The x-coordinate of the center
* @param cy The y-coordinate of the center
* @param color0 The color to use at the start of the sweep
* @param color1 The color to use at the end of the sweep
*/
public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) ;
引數說明:
cx,cy 漸變中心座標。
color0,color1:漸變開始結束顏色。
colors,positions:類似LinearGradient,用於多顏色漸變,positions為null時,根據顏色線性漸變。
兩種顏色漸變
建構函式:
SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1)
sweepGradient = new SweepGradient(450,450,Color.RED, Color.GREEN);
mPaint.setShader(sweepGradient);
canvas.drawCircle(450,450,450,mPaint);
多顏色掃描漸變
建構函式:
SweepGradient(float cx, float cy,@NonNull @ColorInt int colors[], @Nullable float positions[])
int [] colors = {Color.BLACK,Color.RED, Color.BLUE,Color.GREEN};
sweepGradient = new SweepGradient(450,450,colors,null);
mPaint.setShader(sweepGradient);
canvas.drawCircle(450,450,450,mPaint);
設定position:
position陣列設定主要作用是特定位置對應顏色陣列,位置取值【0-1】,0表示開始位置,1表示結束位置,陣列和顏色陣列一一對應。
int [] colors = {Color.BLACK,Color.RED, Color.BLUE,Color.GREEN};
float[] postions = {0f,0.1f,0.7f,1.0f};
sweepGradient = new SweepGradient(450,450,colors,postions);
mPaint.setShader(sweepGradient);
canvas.drawCircle(450,450,450,mPaint);
可以明顯看到BLUE藍色變多。
旋轉掃描例項:
此處只是示例程式碼,如果要真實現可以利用屬性動畫或者handler更新,
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
startAngle = startAngle +1;
if (startAngle == 360){
startAngle = 0;
}
int [] colors = {Color.parseColor("#2300FF00"),Color.parseColor("#7F00FF00")};
float[] position = new float[2];
position[0] = (startAngle * 1f)/360f;
position[1] = ((startAngle + 60)* 1.0f)/360f;
sweepGradient = new SweepGradient(450,450,colors,position);
mPaint.setShader(sweepGradient);
canvas.drawCircle(450,450,450,mPaint2);
canvas.drawCircle(450,450,350,mPaint2);
canvas.drawCircle(450,450,200,mPaint2);
canvas.drawCircle(450,450,100,mPaint2);
float[] lines = {0, 450, 450, 450, 450,450,900, 450, 450, 0, 450, 450,450,450, 450, 900};
canvas.drawLines(lines,mPaint2);
canvas.drawArc(0,0,900,900,startAngle,endAngle,true,mPaint);
invalidate();
}
繪製漸變圓弧
此處只是示例程式碼,如果要真實現可以利用屬性動畫或者handler更新,
endAngle = endAngle +1;
if (endAngle >= 360){
endAngle = 0;
}
int [] colors = {Color.parseColor("#FFFFFF00"),Color.parseColor("#FFFF0000")};
float[] position = new float[2];
position[0] = 0f;
position[1] = ((endAngle)* 1.0f)/360f;
sweepGradient = new SweepGradient(470,470,colors,position);
mPaint.setShader(sweepGradient);
canvas.drawCircle(470,470,450,mPaint2);
canvas.drawArc(20,20,920,920,startAngle,endAngle,false,mPaint);
invalidate();
可以看到,由於設定了positions陣列,繪製多少圓弧,顏色是從圓弧開始繪製的地方漸變到圓弧結束弧度。
positions陣列的設定也有講究,需要開始繪製的顏色對應positions陣列值為0f,結束位置為((endAngle)* 1.0f)/360f;
如果不設定positions陣列引數,結果如下:
endAngle = endAngle +1;
if (endAngle >= 360){
endAngle = 0;
}
int [] colors = {Color.parseColor("#FFFFFF00"),Color.parseColor("#FFFF0000")};
float[] position = new float[2];
position[0] = 0f;
position[1] = ((endAngle)* 1.0f)/360f;
sweepGradient = new SweepGradient(470,470,colors,null);
mPaint.setShader(sweepGradient);
canvas.drawCircle(470,470,450,mPaint2);
canvas.drawArc(20,20,920,920,startAngle,endAngle,false,mPaint);
invalidate();
可以看到圓弧的漸變顏色一直是從開始到整個圓結束,所以和不設定positions陣列有較大區別。