gpt4 book ai didi

android - imageview 旋转动画没有调用 onAnimationEnd

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:53 31 4
gpt4 key购买 nike

我有两个 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 - imageview 旋转动画没有调用 onAnimationEnd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43232768/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com