为什么图像旋转动画只能在第一次正常工作 [英] Why image rotation animation only works correctly for the first time

查看:90
本文介绍了为什么图像旋转动画只能在第一次正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的箭头图像旋转动画,仅在第一次使用时才起作用.从第二次开始,仍然可以旋转,但动画速度不慢.

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.

这是动画xml文件中的代码

Here's the code in anim xml files

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

            }
        });
    }

推荐答案

而不是setAnimation,请使用startAnimation

Instead of setAnimation use startAnimation

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

将动画附加到视图上/或添加视图后,似乎会调用

setAnimation.

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

即使已添加视图,也会一直调用StartAnimation.

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

这篇关于为什么图像旋转动画只能在第一次正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆