为什么图像旋转动画只能在第一次正常工作

Why image rotation animation only works correctly for the first time

我有这个简单的箭头图像旋转动画,它只是第一次按预期工作。从第二次开始,它仍然在旋转,但没有缓慢的动画。

这是动画 xml 文件中的代码

旋转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 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"
    />

卡片视图里面的图片视图。

<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 触发动画的代码。

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);
                }

            }
        });
    }

使用 startAnimation 代替 setAnimation

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

setAnimation 似乎在您将动画附加到视图/或添加视图时被调用。

即使已经添加了视图,StartAnimation 也会一直被调用。