簡體   English   中英

如何在java中重復Android animation?

[英]How to repeat Android animation in java?

我這里有這段代碼。 我希望 Imageview 執行 animation 5 次。 我需要如何設置 setRepeatCount? 如何滅菌?

private void flipCoin() {
    final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
    final ImageView iv = ((ImageView) findViewById(R.id.imageView));
    iv.setRotationY(0f);
    //iv.animate().setDuration(10);
    iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            iv.setImageDrawable(drawable);
            iv.setRotationY(270f);
            iv.animate().rotationY(360f).setListener(null);

        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }
    });

方法 setRepeatCount 屬於Animation class,你正在使用Animator

試試這個:

int times = 5;

private void flipCoin() {
    final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
    final ImageView iv = findViewById(R.id.imageView);
    iv.setRotationY(0f);
    //iv.animate().setDuration(10);
    final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
    viewPropertyAnimator.rotationY(90f);
    viewPropertyAnimator.setListener(new Animator.AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {

            times--;
            if (times > 0) {
                iv.setImageDrawable(drawable);
                iv.setRotationY(270f);
                viewPropertyAnimator.rotationY(360f);
                viewPropertyAnimator.start(); //Restart
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }
    });

    viewPropertyAnimator.start(); //Init
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM