android - onAnimationEnd 没有被调用以进行 imageview 旋转动画

标签 android

我有两个 ImageView,一个顺时针旋转,另一个逆时针旋转。 相同的代码适用于其他动画,但对于旋转,onAnimationEnd 不会被调用。

此处未调用 onAnimationEnd。

public class ObcActivity extends AppCompatActivity implements Animation.AnimationListener {

    ImageView circularImageView1;
    ImageView circularImageView2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(  R.layout.activity_obc);
//        setContentView(  new HeartbeatView(this));

        clockAnimation=AnimationUtils.loadAnimation(this, R.anim.rotate_clockwise);
        antiClockAnimation =AnimationUtils.loadAnimation(this, R.anim.rotate_anticlockwise);
        clockAnimation.setAnimationListener(this);
        antiClockAnimation.setAnimationListener(this);
        clockAnimation.setRepeatCount(-1);
        antiClockAnimation.setRepeatCount(-1);
        clockAnimation.setRepeatMode(Animation.INFINITE);
        antiClockAnimation.setRepeatMode(Animation.INFINITE);
        circularImageView1= (ImageView) findViewById(R.id.circularImageView1);
        circularImageView2= (ImageView) findViewById(R.id.circularImageView2);
        circularImageView1.setAnimation(clockAnimation);
        circularImageView1.startAnimation(clockAnimation);
        circularImageView2.setAnimation(antiClockAnimation);

    }


    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        Toast.makeText(this,""+System.currentTimeMillis(),Toast.LENGTH_SHORT).show();
        if(animation==clockAnimation){
            circularImageView1.setVisibility(View.INVISIBLE);
            circularImageView2.setVisibility(View.VISIBLE);
            circularImageView1.clearAnimation();
            circularImageView2.startAnimation(clockAnimation);
        }else {
            circularImageView2.setVisibility(View.INVISIBLE);
            circularImageView1.setVisibility(View.VISIBLE);
            circularImageView1.startAnimation(antiClockAnimation);
            circularImageView2.clearAnimation();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    Animation clockAnimation, antiClockAnimation;

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#71bf44"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/circularImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:src="@drawable/ic_circle"
        />
    <ImageView
        android:id="@+id/circularImageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:src="@drawable/outer_ring_2_white"
        />
    <ImageView
        android:id="@+id/circularImageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:src="@drawable/outer_ring_3_white"
        />

    <ImageView
        android:id="@+id/circularImageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:src="@drawable/outer_ring_2_white_out"
        />
    <ImageView
        android:id="@+id/circularImageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:src="@drawable/outer_ring_3_white_out"
        />




    </RelativeLayout>

enter image description here 更新:

根据答案,我设置了以下不起作用的代码,没有调用 onAnimationEnd。我需要获取第一张图像的动画何时结束的事件!

clockAnimation.setRepeatCount(100);
        antiClockAnimation.setRepeatCount(100);
        clockAnimation.setRepeatMode(100);
        antiClockAnimation.setRepeatMode(100);

最佳答案

因为你在Animation中设置了INFINITE

clockAnimation.setRepeatMode(Animation.INFINITE);

它将以无限模式开始,意味着永远不会结束

您的动画迭代更新将在 onAnimationRepeat 中通知

@Override
public void onAnimationRepeat(Animation animation) {

}

什么document onAnimationEnd方法的说法

Notifies the end of the animation. This callback is not invoked for animations with repeat count set to INFINITE.

根据评论:

But I need animation end event. for example after 2 rotations.

为此在代码中添加此行

clockAnimation.setRepeatCount(2); 
clockAnimation.setRepeatMode(Animation.RESTART);

关于android - onAnimationEnd 没有被调用以进行 imageview 旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43232768/

相关文章:

android - 如何从 xml 布局父级获取样式属性

java - LIstView SectionIndexer 给出错误 ArrayIndexOutOfBoundsException

安卓自定义布局

android - 如何在我的 Android 应用程序本身中使用 Mockito?我收到 Hamcrest 依赖冲突,当它仅用于测试时我没有收到

Android LiveData 列表未更新

java - 如何使用appium水平滑动

android - java.lang.NoClassDefFoundError : com. 谷歌.android.gms.common.internal.zzbp

android - 将多个文件传递给另一个应用程序

java - Eclipse 在构建 android 项目时删除 java 文件

android - 如何实现惰性列一次滚动一列的功能