在Android上双启动onAnimationEnd

时间:2013-01-30 11:58:18

标签: android animation

我正在尝试制作一种幻灯片。为此,我需要在每个动画结束时通过更改图像来使循环成为动画。但无论出于何种原因,同一事件每个动画发生两次

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

2 个答案:

答案 0 :(得分:6)

你的动画集由两个动画组成,每个动画都会调用你的代码,为了解决这个问题,你可以这样做:

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

insted of

animationSlide.setAnimationListener(.....);

答案 1 :(得分:1)

我的解决办法是从 .setAnimationListener 更改为 .withEndAction

示例:

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

说明:

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