android取消动画导致stackoverflow错误

时间:2015-02-06 04:08:46

标签: android animation cancellation

我正在尝试使用动画来缩放图像。但似乎有一些问题,我不知道为什么它出现....

请帮我解决这个问题...

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;
                }
            });

我不知道它何时会导致结束动画出错。请帮帮我

更新:

对象动画片不能用作其他动画,它不能像我预期的那样回到原始大小。我想是的,如果其他人知道怎么解决这个问题,请教我解决问题。很多,谢谢

2 个答案:

答案 0 :(得分:2)

  @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;
        }
    });
}
}

答案 1 :(得分:0)

AnimatorSet.end()触发onAnimationEnd(Animator)因此递归。

请参阅documentation

  

结束动画。这会导致动画分配动画属性的结束值,然后在其侦听器上调用onAnimationEnd(Animator)方法