onAnimationEnd不被调用

时间:2017-04-30 07:14:16

标签: android animation objectanimator animatorset

因为动画集没有提供 REPEAT 功能, 我正在实施自己的重复逻辑。

但是我有问题。

首先,让我向您展示我的示例代码,如下所示。

我的主要活动 - >

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button button;

    private AnimatorSet animatorSet;
    private Repetition repetition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_view);
        button = (Button) findViewById(R.id.button);

        animatorSet = new AnimatorSet();
        repetition = new Repetition();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimation();
            }
        });
    }

    private void startAnimation() {
        imageView.post(new Runnable() {
            @Override
            public void run() {
                final ObjectAnimator moveY = ObjectAnimator.ofFloat(imageView, "y", imageView.getY(), imageView.getY() - 200F);
                moveY.setDuration(1000);

                final ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);
                rotation.setDuration(1000);

                repetition.repeat(animatorSet, 10);

                List<Animator> list = new ArrayList<>();

                list.add(moveY);
                list.add(rotation);

                // problem is here, if i run a single animator, repetition class can not get onAnmationEnd callback.
                animatorSet.play(moveY);

                // if i run playSequentially with multiple animators it works properly;
//                animatorSet.playSequentially(list);
                animatorSet.start();
            }
        });
    }

}

我的REPETITION支持类 - &gt;

它正在聆听动画师的生命周期,如果动画结束,它会将期望的动画重复次数与onAnimationEnd回调中的当前重复次数进行比较。如果我运行一个动画师.. onAnimationEnd不会被调用...

第一次onAnimationEnd被称为..

public class Repetition implements Animator.AnimatorListener {

    private AnimatorSet animatorSet;
    private int repeatCount;

    private int currentRepeatCount;

    public void repeat(AnimatorSet animatorSet, int repeatCount) {
        this.animatorSet = animatorSet;
        this.repeatCount = repeatCount;

        animatorSet.removeAllListeners();
        animatorSet.addListener(this);
    }

    @Override
    public void onAnimationStart(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationStart");
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if(currentRepeatCount < repeatCount) {
            animatorSet.start();
            currentRepeatCount++;
            Log.d("RepeatitionLog", "onAnimationRepeat by repetition");
            Log.d("RepeatitionLog", "currentRepeatCount : " + currentRepeatCount);
        } else {
            Log.d("RepeatitionLog", "onAnimationEnd");
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationCancel");
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        Log.d("RepeatitionLog", "onAnimationRepeat");
    }
}

我很感激任何帮助!

感谢。

0 个答案:

没有答案