问题
Hi, I m trying to draw a circle as shown in the picture.where my values ranges from 0-100. 0-40 green 41-60 yellow 61-80 orange and 81-100 red.
the view should animate from 0 to defined value slowly,this one to achieve.
I have tried the following code ,and trying to achieve, code as follows:
public class GradiantActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GradientTestView(this));
}
private class GradientTestView extends View {
private Paint mPaint;
private final int[] mColors;
private int CENTER_X;
private int CENTER_Y;
private Shader shader;
/**
* Radius coordinates to draw circle.
*/
float r;
public GradientTestView(Context context) {
super(context);
mColors = new int[] {
0xFF00FF00, //Green
0xFFFFFF00, //Yellow
//Orange
0xFFFF0000 //Red
};
shader = new SweepGradient(0, 0, mColors, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(100);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.translate(CENTER_X, CENTER_Y);
mPaint.setShader(shader);
canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
//for different positioning
//canvas.translate(0, CENTER_Y);
//canvas.translate(-CENTER_X * 3 / 4, -CENTER_Y / 2);
//canvas.translate(CENTER_X * 3 / 2, 0);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
CENTER_X = this.getWidth() / 2;
CENTER_Y = this.getHeight() / 2;
r = CENTER_Y / 2 - mPaint.getStrokeWidth() * 0.5f;
}
}
}
Please help in animate this view from 0 to any defined value.
来源:https://stackoverflow.com/questions/19428477/androidcircle-with-sweepgradient-to-animate