Search code examples
androidandroid-animationandroid-custom-view

Change costum View color between colors animated


I have a custom View which I draw an arc that represents the progress of an event like passed time between two dates. It looks like this:

enter image description here

I want to change stroke color of the arc between my arc coolor and red as time passes.

I have tried following but it doesn't work:

remainingTimeProgress = (DonutProgress) rootView.findViewById(R.id.remainingTimeIndicator);
Integer colorFrom = getResources().getColor(R.color.donut_finished_color);
Integer colorTo = getResources().getColor(R.color.Red);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(currentTimeLength);
long passedTime = Calendar.getInstance().getTime().getTime() - currentDate.getTime();
colorAnimation.setCurrentPlayTime(passedTime);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(final ValueAnimator animator) {

        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Integer colorValue = (Integer) animator.getAnimatedValue();
                remainingTimeProgress.setFinishedStrokeColor(colorValue);
            }
        });
    }

});
colorAnimation.start();

Here is my custom View code: http://pastebin.com/ERyLiczg

How can I do that?

UPDATE

I have experimented with some hard coded values for animation time like:

colorAnimation.setDuration(15000);
colorAnimation.setCurrentPlayTime(0);

And it has worked. But when I set the play time dynamically to passed time it doesn't change the color to color that should be at that time.

Do you have any idea what is the problem?

UPDATE

Setting current play time after start solved the problem.

colorAnimation.start();
colorAnimation.setCurrentPlayTime(passedTime);

Solution

  • Call set current play time after start:

    colorAnimation.start();
    colorAnimation.setCurrentPlayTime(passedTime);