I'm writing an app where I'm having some views in a RelativeLayout (in different positions, use margins) and I'm animating them using the new animation API in Honeycomb. The animations are repeating themselves but they need to wait some time between every repetition, so I can't use repeat mode.
All goes well but there's a part when I want to move them to another place and stop the animations, but it refuses to stop. I'm moving them and they don't appear and suddenly I see them passing by as they're still being animated. I tried any 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 every image I'm creating an Animator that has listener and on every animation end, the listener being called and a new animation is created and getting start delay. I store all the animations in a 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 moving 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 tried about hundred ways!
NOTE: THIS IS HONEYCOMB API I'M USING ANIMATOR NOT ANIMATION OF PRE-3.0