Reputation: 903
I'm trying to create a sort of slide show. For this I need to make the loop the animation with the change of the image at the end of each animation. But for whatever reason, the same event occurs twice per 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);
}
In 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>
in log:
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
Upvotes: 6
Views: 3480
Reputation: 6090
My solution for this was to change from .setAnimationListener to .withEndAction
Example:
my_button.animate()
.setDuration(10)
.scaleX(1)
.scaleY(1)
.withEndAction(() -> (your action goes here) ).start();
Explanation:
.withEndAction() is runnable gets removed after it was started. So The Runnable is just executed once and the Listener might be called multiple times.
Upvotes: 2
Reputation: 9296
Your animationset consists of two animations , and your code is being called by each one of them, to fix this you can do this:
animationSlide.getAnimations().get(1).setAnimationListener(.....);
insted of
animationSlide.setAnimationListener(.....);
Upvotes: 6