我有两个 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>
根据答案,我设置了以下不起作用的代码,没有调用 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/