onAnimationEnd没有调用imageview旋转动画

时间:2017-04-05 13:33:38

标签: 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);

2 个答案:

答案 0 :(得分:2)

因为您在Animation

中设置了 INFINITE
clockAnimation.setRepeatMode(Animation.INFINITE);

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

您的动画迭代更新将在onAnimationRepeat

中通知
@Override
public void onAnimationRepeat(Animation animation) {

}

方法 onAnimationEnd

document说法
  

通知动画结束。不会调用此回调   重复计数设置为INFINITE的动画。

根据评论

  

但我需要动画结束活动。例如,在2次旋转之后。

为此添加代码

中的这一行
clockAnimation.setRepeatCount(2); 
clockAnimation.setRepeatMode(Animation.RESTART);

答案 1 :(得分:0)

查看INFINITE动画的documentation

  

void onAnimationEnd(动画动画)

     

通知动画结束。不会调用此回调   重复计数设置为INFINITE的动画。

无论如何,我建议保留两个Animation.AnimationListener个对象并在您的活动中实现一个。这样可以使您的代码更加清晰,无需提问if(animation==clockAnimation)等问题。

相关问题