градиентная дуга в зависимости от угла - PullRequest
0 голосов
/ 02 января 2019

Я хочу подражать рейтингу. Если это плохая оценка, цвет должен быть красным, хороший - зеленым ... Я могу добиться цветовой анимации от красного до зеленого, но в конце она всегда заканчивается зеленым. Я должен что-то делать .. Мне нужно, чтобы изменение цвета зависело от угла (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;
    }
}
...