kemdo
kemdo

Reputation: 1459

android cancel animation cause stackoverflow error

i am trying to use animation to zoom image. But it seem there have some problem i can not know why it appear....

Please help me solve this problem...

imgAlpha.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub

                    List<Animator> animations = new ArrayList<Animator>();
                    animations.add(ObjectAnimator.ofFloat(imgAlpha,
                            View.SCALE_X, 2).setDuration(800));
                    animations.add(ObjectAnimator.ofFloat(imgAlpha,
                            View.SCALE_Y, 2).setDuration(800));
                    final AnimatorSet animatorSet = new AnimatorSet();
                    animatorSet.playTogether(animations);
                    animatorSet.start();
                    animatorSet.setDuration(800);
                    animatorSet.addListener(new AnimatorListener() {

                        @Override
                        public void onAnimationStart(Animator animation) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onAnimationEnd(final Animator animation) {
                            // TODO Auto-generated method stub
                            // TODO Auto-generated method stub
                            Logger.error("end animation");
                            animatorSet.cancel();
                            imgAlpha.clearAnimation();
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                            // TODO Auto-generated method stub
                            Logger.error("calcel animation");
                        }
                    });
                    return true;
                }
            });

I do not know when it cause error on end animation. please help me

UPDATE:

OBJECT ANIMATOR CAN NOT USE AS OTHER ANIMATION, IT CAN NOT BACK TO ORIGINAL SIZE AS I EXPECT. I THINK SO, IF OTHER PEOPLE KNOW HOW TO SOLVE IT, PLEASE TEACH ME OUT OF PROBLEM. MANY THANKS

Upvotes: 2

Views: 1216

Answers (2)

Md Hussain
Md Hussain

Reputation: 409

  @Override
                public void onAnimationEnd(final Animator animation) {
                    // TODO Auto-generated method stub
                    // TODO Auto-generated method stub

                    Log.e("error", "end animation");
                   **// animatorSet.cancel();**
                    img.clearAnimation();
                }



package com.example.count;

import java.util.ArrayList;
import java.util.List;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class main extends Activity{
    Button b1;
    ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);
    img=(ImageView)findViewById(R.id.imageView1);
    img.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub

            List<Animator> animations = new ArrayList<Animator>();
            animations.add(ObjectAnimator.ofFloat(img,
                    View.SCALE_X, 2).setDuration(800));
            animations.add(ObjectAnimator.ofFloat(img,
                    View.SCALE_Y, 2).setDuration(800));
            final AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(animations);
            animatorSet.start();
            animatorSet.setDuration(800);
            animatorSet.addListener(new AnimatorListener() {

                @Override
                public void onAnimationStart(Animator animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(final Animator animation) {
                    // TODO Auto-generated method stub
                    // TODO Auto-generated method stub

                    Log.e("error", "end animation");
                   // animatorSet.cancel();
                    img.clearAnimation();
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    // TODO Auto-generated method stub

                    Log.e("error", "calcel animation");
                }
            });
            return true;
        }
    });
}
}

Upvotes: 2

Attacktive
Attacktive

Reputation: 617

AnimatorSet.end() fires onAnimationEnd(Animator) hence a recursion.

See documentation:

Ends the animation. This causes the animation to assign the end value of the property being animated, then calling the onAnimationEnd(Animator) method on its listeners.

Upvotes: 1

Related Questions