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:
-
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?
-
Because of clearAnimation(), onAnimationEnd() is getting called multiple times and I’m facing problems in running animations one after another.
-
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++;
}
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
To actually move the position of the object, look into using a modified version of the below code snippet.
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
or
Sample Code Below:
This code is copied and pasted from a project of mine but it has been changed somewhat, should still run.