Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop animation in Android

Requirement:

I need to run multiple animations one by one. I'm using onAnimationEnd() to do so.

While animating, in case of a touch event, I need to stop the animations and display a new image in that location.

Problems:

  1. I'm using clearAnimation() in case of onTouch. Because of this, the complete animation got removed. My intention is to stop the animation and and display a new image in the touched portion. How can I achieve this?

  2. Because of clearAnimation(), onAnimationEnd() is getting called multiple times and I'm facing problems in running animations one after another.

  3. Is there any function just to stop the animation instead of clearing it completely? I am using Android 2.1.

@Override
public boolean onTouch(View v, MotionEvent event) {      
    switch(event.getAction()) {
        case MotionEvent.ACTION_UP:
            imageArray[g_animCount - 1].clearAnimation();          
            break;

        default:
            break;
    }

    return true; // indicate event was handled
}

@Override
public void onAnimationEnd(Animation animation) {
    layout.removeView(imageArray[g_animCount - 1]);
    if ( (g_animCount < count))
    {               
        startNextAnimation();
    }
    else
    {
        g_animCount = 0;
        isBegin = false;
    }
}

public void startNextAnimation()
{
     int j = random.nextInt(200);
     layoutParams.setMargins(j, -20, 30, 0);
     layout.addView(imageArray[g_animCount], layoutParams); 
     imageArray[g_animCount].startAnimation(movArray[g_animCount]);
     g_animCount++;
}
like image 205
pavan Avatar asked Mar 31 '12 04:03

pavan


People also ask

How do I turn off infinite animation on Android?

Use clearAnimation() to stop an animation.


1 Answers

An animation only moves the pixels on the screen, not the position of the object. To set your to stay where it upon end, set your

animation.setFillAfter(true);

To actually move the position of the object, look into using a modified version of the below code snippet.

MarginLayoutParams marginParams = new MarginLayoutParams(object.getLayoutParams());
marginParams.setMargins(left, (top+hol2), left, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
object.setLayoutParams(layoutParams);

Regarding the onAnimationEnd being called multiple times, I would need to see some code.

The only two ways I know of to manually stop an animation would be

animation.cancel(); (may not work for 2.1, can't remember)

or

object.clearAnimation();

Sample Code Below:

    upmotionleft = new TranslateAnimation(0, 0, 0, 600);
    upmotionleft.setDuration(2000);
    upmotionleft.setFillEnabled(true);
    upmotionleft.setAnimationListener(new Animation.AnimationListener() 
    {
        @Override
        public void onAnimationStart(Animation animation) 
        {}
        @Override
        public void onAnimationEnd(Animation animation) 
        {
            //sets to wherever I want the final object to be
            MarginLayoutParams marginParams = new MarginLayoutParams(object.getLayoutParams());
            marginParams.setMargins(left, top-hol2, left, 0);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
            object.setLayoutParams(layoutParams);

            //starts next animation
            object.startAnimation(nextAnimation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) 
        {}
    });

object.startAnimation(upmotionleft);

This code is copied and pasted from a project of mine but it has been changed somewhat, should still run.

like image 182
testingtester Avatar answered Sep 21 '22 16:09

testingtester