Can't stop animator in android after starting it
I am writing an application where I have some views in a RelativeLayout (in different positions, use margins) and I am animating them using the new animation API in Honeycomb. The animations are repeating, but they need to wait a while between each repetition, so I cannot use the repeat mode.
Everything is going well, but there is a part where I want to move them to a different location and stop the animation, but it refuses to stop. I move them around and they don't appear, and suddenly I see them pass by as they are still alive. I have tried every possible way I could think of, please help me.
code:
if(!mMoving){
mMoving = true;
for(int i = 0; i < mImagesList.size(); i++){
final LinearLayout f = mImagesList.get(i);
if(mMoving){
ObjectAnimator anim = ObjectAnimator.ofFloat(f, "x", Math.round(mScreenWidth * 1.4));
mAnimators.add(anim);
anim.setDuration(mRandom.nextInt(10000) + 8000);
anim.setStartDelay((mRandom.nextInt(4000) + 3000) * (i / ITEMS_PER_SCREEN));
anim.addListener(new AnimatorListener() {
@Override
public void onAnimationEnd(Animator animation) {
if(mMoving){
mAnimators.remove(animation);
ImageView img = (ImageView)f.findViewById(R.id.stream_feed_item_pic);
int picWidth = img.getDrawable().getIntrinsicWidth();
Animator anim = ObjectAnimator.ofFloat(f, "x", -Math.round(picWidth * 1.4), Math.round(mScreenWidth * 1.2));
mAnimators.set(mAnimators.indexOf(animation), anim);
anim.setDuration(mRandom.nextInt(14000) + 8000);
anim.setStartDelay((mRandom.nextInt(6000) + 3000) * (mImagesList.size() / ITEMS_PER_SCREEN));
anim.addListener(this);
anim.start();
}
}
});
anim.start();
}
}
mMoving = true;
return true;
}
As you can see, for each image, I create an Animator that has a listener and at each end of the animation, a caller listener and a new animation are created and get a start delay. I keep all the animations in the list.
This is my (desperate) attempt to stop them:
if(mMoving){
mMoving = false;
for(Animator anim : mAnimators){
anim.setStartDelay(0);
anim.setDuration(0);
anim.start();
anim.cancel();
anim.removeAllListeners();
anim.setTarget(null);
}
mAnimators.clear();
}
This is how I move them to another layout:
mContainer.removeAllViews();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(picWidth, picHeight);
params.leftMargin = 0;
params.topMargin = 0;
if(size == SMALL_SIZE){
if(mSmallCounter < mSmallIdList.length){
RelativeLayout frame = (RelativeLayout)findViewById(mSmallIdList[mSmallCounter++]);
frame.addView(f, params);
}
}
I'm very desperate, I've tried about a hundred ways!
NOTE: THIS IS A HONEYCOMB PROGRAM I USE ANIMATOR, NOT ANIMATION PRE-3.0
source to share