Android上的双启动onAnimationEnd
我正在尝试制作一种幻灯片。为此,我需要使循环成为动画,并在每个动画结束时更改图像。但无论出于何种原因,同一事件在每个动画中都会发生两次Android上的双启动onAnimationEnd,android,animation,Android,Animation,我正在尝试制作一种幻灯片。为此,我需要使循环成为动画,并在每个动画结束时更改图像。但无论出于何种原因,同一事件在每个动画中都会发生两次 public cPhotoSlide(Activity _activity, DBHelper _helper, int idMenu, cItemViewer _ItemViewer){ activity = _activity; helper = _helper; ImageManager = h
public cPhotoSlide(Activity _activity, DBHelper _helper, int idMenu, cItemViewer _ItemViewer){
activity = _activity;
helper = _helper;
ImageManager = helper.ImageManager;
ItemViewer = _ItemViewer;
cursor = 0;
itemIds = ChildsItemsToTable(idMenu);
MainLayout = (RelativeLayout) activity.findViewById(R.id.fon);
SliderObj = activity.getLayoutInflater().inflate(R.layout.photo_slide, MainLayout, true);
SlideImage = (ImageView) SliderObj.findViewById(R.id.slide_image);
SlideImage.setImageBitmap(ImageManager.loadImage(activity.getApplicationContext(),itemIds.get(cursor)));
animationSlide = (AnimationSet) AnimationUtils.loadAnimation(activity.getApplicationContext(), R.anim.slide);
animationSlide.getAnimations().get(1).setAnimationListener(
new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
Log.d(LOG_TAG,"Cursor = "+cursor+"/"+itemIds.size());
if (cursor >= itemIds.size()-1) {
cursor = 0;
} else {
cursor += 1;
}
if (itemIds.get(cursor).content != 0) {
SlideImage.setImageBitmap(ImageManager.loadImage(itemIds.get(cursor)));
}
SlideImage.startAnimation(animationSlide);
}
}
);
SlideImage.startAnimation(animationSlide);
}
在XML中:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<alpha
android:duration="250"
android:fromAlpha="0.0"
android:toAlpha="1.0">
</alpha>
<alpha
android:startOffset="2000"
android:duration="250"
android:fromAlpha="1.0"
android:toAlpha="0.0">
</alpha>
</set>
您的animationset由两个动画组成,其中每一个都在调用您的代码。要解决此问题,您可以执行以下操作:
animationSlide.getAnimations().get(1).setAnimationListener(.....);
代替
animationSlide.setAnimationListener(.....);
我的解决方案是将.setAnimationListener更改为.withEndAction 例如:
my_button.animate()
.setDuration(10)
.scaleX(1)
.scaleY(1)
.withEndAction(() -> (your action goes here) ).start();
说明:
.withEndAction()是可运行的,在启动后将被删除。因此,Runnable只执行一次,侦听器可能会被调用多次。我在您的代码片段中没有看到您的问题。您如何得出onAnimationEnd()被触发两次的结论?@class stacker我在日志计时中定义了它也许我加载这个动画是错误的
animationSlide=(AnimationSet)AnimationUtils.loadAnimation(activity.getApplicationContext(),R.anim.slide)
因为现在动画立即关闭,忽略:android:startOffset=“2000”
发布了该类的所有构造函数请不要在布局xml中设置ImageBitmap并设置imageview大小,android:layout\u width=“50dp”android:layout\u height=“50dp”奇怪,但在第一个动画中添加了android:startOffset=“100”
后,问题就解决了。不过,请注意,只有在动画正常结束时才会运行该动作;如果在该动画期间取消ViewPropertyAnimator,则runnable将不会运行。
因此,如果例如在动画之后应启用视图,这不是一个好地方,因为无法保证执行。
my_button.animate()
.setDuration(10)
.scaleX(1)
.scaleY(1)
.withEndAction(() -> (your action goes here) ).start();