How to stop the animation when needed and start it instantly in android
I’m new to android and I need help.
When I click on a button on my keyboard that has the same label as the image, I expect the image that falls from the top of the screen to become invisible. Although I have implemented the above points, the problem arises when I want to show the next animation again immediately after the previous image is not visible. Here is my animation code.
public void startAnimation(final ImageView aniView)
{
animator= ValueAnimator.ofFloat(0 ,.85f);
animator.setDuration(Constants.ANIM_DURATION);
animator.setInterpolator(null);
generation of random values
Random rand = new Random();
index = rand.nextInt((int) metrics.widthPixels);
for debugging
i = Integer.toString(index);
Log.e(" index is :", i);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
float value = ((Float) (animation.getAnimatedValue())).floatValue();
aniView.setTranslationX(index);
aniView.setTranslationY((height+ (50*mScale))*value);
aniView.setTranslationY(height);
}
});
animator.start();
}
keyboard methods goes here
@Override
public void onClick(View v)
{
gettext(v);
}
private void gettext(View v)
{
try
{
String b = "";
b = (String) v.getTag();
String img_val = map.get(b);
int imgid = (Integer) imageView.getTag();
Log.e("img values=:", img_val+" "+imgid);
if(img_val.equals(ALPHABETS[imgid]))
{
imageView.setVisibility(View.INVISIBLE);
animator.start();
trying to start the animation again
}
I’m using animation.addupdateListener to invoke the animation again and again, but it only calls when the .ofFloat completes the value. When I click the appropriate button, the animation becomes invisible, but the next animation starts again at the same time. I want to start right away.
Solution
Basically calling start() again won’t work. You need to somehow “reset” the animation, have you tried calling end() before?
animator.end();
animator.start();