Claudiu Sarmaşi
Claudiu Sarmaşi

Reputation: 15

How can i change the background of an AppCompatButton while its rotating?

I'm using a RotateAnimation to rotate an AppCompatButton, used as a toolbar button, and i want to change its background while is rotating.

I didn't find any useful topic so far.

Here's my code :

AppCompatButton mBtn = (AppCompatButton) view.findViewById(R.id.search_btn);
        Animation mRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
mBtn.setAnimation(mRotateAnimation);
mBtn.startAnimation(mRotateAnimation);

rotation.xml:

<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:duration="600" />

I want to get a smooth background change between the beginning and the end of animation. Any useful resource link or code would be amazing. Thank you guys!

Upvotes: 0

Views: 97

Answers (1)

Marat Zangiev
Marat Zangiev

Reputation: 1402

You should add another ValueAnimation to your view.

AppCompatButton mBtn = (AppCompatButton) view.findViewById(R.id.search_btn);
        Animation mRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation);
mBtn.setAnimation(mRotateAnimation);
mBtn.startAnimation(mRotateAnimation);

int colorFrom = getResources().getColor(R.color.red);
int colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(600);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        mBtn.setBackgroundColor((int) animator.getAnimatedValue());
    }

});
colorAnimation.start();

Upvotes: 2

Related Questions