我想模仿评分栏。如果评级不佳,则颜色应为红色,绿色为好。
我可以实现从红色到绿色的彩色动画,但最终总是以绿色结尾。
我必须做些事情。我需要使颜色变化取决于角度(angleRating
),而不是始终以绿色结尾。
片段:
CircleAngleAnimation animation = new CircleAngleAnimation(ratingCircleBar, angleRating, RATING_ANIMATION_DURATION);
ratingCircleBar.startAnimation(animation);
CircleAngleAnimation:
public class CircleAngleAnimation extends Animation {
private Circle circle;
private float oldAngle;
private float newAngle;
private int duration;
public CircleAngleAnimation(Circle circle, int newAngle, int duration) {
this.oldAngle = circle.getAngle();
this.newAngle = newAngle;
this.circle = circle;
this.duration = duration;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation transformation) {
float angle = oldAngle + ((newAngle - oldAngle) * interpolatedTime);
setDuration(duration);
circle.setAngle(angle);
circle.requestLayout();
}
}
圈子自定义视图:
public class Circle extends View {
private static final int START_ANGLE_POINT = 90;
private final int[] COLORS = { getResources().getColor(R.color.red), getResources().getColor(R.color.yellow), getResources().getColor(R.color.green) };
private final Paint paint;
private final RectF rect;
private float angle;
public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
final int strokeWidth = 20;
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(strokeWidth);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), COLORS[0], COLORS[1], COLORS[2]);
colorAnimation.setDuration(3000); // milliseconds
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
paint.setColor((int) animator.getAnimatedValue());
}
});
colorAnimation.start();
rect = new RectF(10, 10, 40 + strokeWidth, 40 + strokeWidth);
angle = 20;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(rect, START_ANGLE_POINT, angle, true, paint);
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
}