5

我正在尝试创建一种幻灯片放映。为此,我需要在每个动画结束时随着图像的变化使循环成为动画。但无论出于何种原因,每个动画都会发生两次相同的事件

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>

在日志中:

01-30 15:52:26.700: D/cPhotoSlide(22301): Cursor = 0/207
01-30 15:52:26.716: D/cPhotoSlide(22301): Cursor = 1/207
01-30 15:52:28.990: D/cPhotoSlide(22301): Cursor = 2/207
01-30 15:52:29.005: D/cPhotoSlide(22301): Cursor = 3/207
01-30 15:52:31.279: D/cPhotoSlide(22301): Cursor = 4/207
01-30 15:52:31.302: D/cPhotoSlide(22301): Cursor = 5/207
01-30 15:52:33.575: D/cPhotoSlide(22301): Cursor = 6/207
01-30 15:52:33.591: D/cPhotoSlide(22301): Cursor = 7/207
01-30 15:52:35.865: D/cPhotoSlide(22301): Cursor = 8/207
01-30 15:52:35.888: D/cPhotoSlide(22301): Cursor = 9/207
01-30 15:52:38.161: D/cPhotoSlide(22301): Cursor = 10/207
01-30 15:52:38.177: D/cPhotoSlide(22301): Cursor = 11/207
4

2 回答 2

6

您的动画集由两个动画组成,并且每个动画都在调用您的代码,要解决此问题,您可以这样做:

animationSlide.getAnimations().get(1).setAnimationListener(.....);

安装的

animationSlide.setAnimationListener(.....);
于 2013-01-30T12:09:04.133 回答
2

我的解决方案是从.setAnimationListener更改为.withEndAction

例子:

 my_button.animate()
     .setDuration(10)
     .scaleX(1)
     .scaleY(1)
     .withEndAction(() -> (your action goes here) ).start();

解释:

.withEndAction() is runnable 在启动后被删除。所以 Runnable 只执行一次,而 Listener 可能会被调用多次。

于 2017-04-06T12:55:46.733 回答