Why image rotation animation only works correctly for the first time

I have this simple arrow image rotation animation which works as intended only for the first time. from second time onward It's still do the rotation but without slow animation.

enter image description here

Here's the code in anim xml files

Rotate 180

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:fromDegrees="0"
    android:toDegrees="180"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:fillAfter="true"
    android:fillEnabled="true"/>

Rotate Revere

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:fromDegrees="180"
    android:toDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:fillAfter="true"
    android:fillEnabled="true"
    />

The image view inside card view.

<ImageView
   android:id="@+id/creadit_card_next_image"
   android:layout_width="@dimen/next_image_size" 
   android:layout_height="@dimen/next_image_size"
   android:layout_marginEnd="@dimen/static_menu_primary_margin"
   android:layout_marginTop="16dp"
   android:rotation="-90"
   android:src="@drawable/ic_navigate_next"
   android:tint="@color/colorPrimary"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintTop_toTopOf="parent" />

Java code to trigger Animation.

private Animation rotatePlus180;
private Animation rotateMinus180;
private boolean creditDebitCardViewExpanded = true;

rotatePlus180 = AnimationUtils.loadAnimation(this, R.anim.rotate_plus_180);
rotateMinus180 = AnimationUtils.loadAnimation(this, R.anim.rotate_minus_180);



private void onClickCreditDebitCardView() {
        creditDebitCardPaymentMethod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (creditDebitCardViewExpanded) {
                    expandAnimation(paymentRecyclerView);
                    creditDebitCardViewExpanded = false;
                    creditCardNextImage.setAnimation(rotatePlus180);
                } else {
                    collapseAnimation(paymentRecyclerView);
                    creditDebitCardViewExpanded = true;
                    creditCardNextImage.setAnimation(rotateMinus180);

                    CreditDebitLayoutContainer.setPadding(0, 0, 0, padding);
                }

            }
        });
    }

Upvotes: 1

Views: 709

Answers (1)

martin
martin

Reputation: 311

Instead of setAnimation use startAnimation

creditCardNextImage.startAnimation(rotatePlus180);
creditCardNextImage.startAnimation(rotateMinus180);

setAnimation seems to be called once you attach the animation to the view/ or when the view is added.

StartAnimation will be called all the time even if the view has already been added.

Upvotes: 2

Related Questions