How to stop animation in android

Demand:

I use several animations and have to run the animations one by one. Animations descend one after another. Currently using onAnimationEnd () to run animations one by one.

While the animation is dropping, in case of a touch, I need to stop the movement of the animation and display a new image in this place.

Problems:

1) Currently, the use of clearAnimation () in the case of onTouch, is removed due to this full animation. My intention is to stop the movement and display the new image in touch. How to achieve this?

2) Due to clearAnimation (), onAnimationEnd () gets called several times and encounters a problem when starting the animation one by one.

3) Is there any function just to stop the movement of the animation, and not to clean it completely, I use the version of Android version 2.

@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++; } 
+6
source share
1 answer

The animation only moves pixels on the screen, not the position of the object. To set where you left off, set

 animation.setFillAfter(true); 

To actually move the position of an object, examine the use of a modified version of the code snippet below.

 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 fact that onAnimationEnd is called multiple times, I will need to see some code.

The only thing I know to manually stop the 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 was copied and pasted from my project, but it has changed a bit, it should still work.

+7
source

Source: https://habr.com/ru/post/912100/


All Articles