ImageView放大、缩小无限动画和onAnimationRepeat问题

Edm*_*mas 1 animation android scale zooming imageview

我想为我的 ImageView 构建一个放大和缩小动画,我设置了一个侦听器并将动画 RepeatCount 设置为无限。

首先,我从放大效果开始,然后在 onAnimationRepeat 方法中创建缩小部分,其中使用布尔值我想重新启动整个效果,开始再次放大。但是在第一次没有再次调用 onAnimationRepeat 之后,反过来动画正在重复但它卡在缩小部分。

我错过了什么?

//image animation
        Animation anim = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(10000);
        zoomIn = true;

        // Start animating the image
        final ImageView splash = (ImageView) findViewById(R.id.imageView);
        splash.startAnimation(anim);

        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                if(zoomIn) {
                    Log.w("", "we zoom out, and zoomIn is: " + zoomIn);
                    Animation anim = new ScaleAnimation(1.1f, 1f, 1.1f, 1f);
                    anim.setInterpolator(new LinearInterpolator());
                    anim.setRepeatCount(Animation.INFINITE);
                    anim.setDuration(10000);
                    splash.startAnimation(anim);
                    zoomIn = false;

                } else if(!zoomIn) {
                    Log.w("", "we zoom in, and zoomIn is: " + zoomIn);
                    Animation anim = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f);
                    anim.setInterpolator(new LinearInterpolator());
                    anim.setRepeatCount(Animation.INFINITE);
                    anim.setDuration(10000);
                    splash.startAnimation(anim);
                    zoomIn = true;
                }


            }
        });


    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*rky 5

只需切换到 ObjectAnimator 并使用以下内容:

ObjectAnimator scaleX = ObjectAnimator.ofFloat(btnSubscribe, "scaleX", 0.9f, 1.1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(btnSubscribe, "scaleY", 0.9f, 1.1f);

scaleX.setRepeatCount(ObjectAnimator.INFINITE);
scaleX.setRepeatMode(ObjectAnimator.REVERSE);

scaleY.setRepeatCount(ObjectAnimator.INFINITE);
scaleY.setRepeatMode(ObjectAnimator.REVERSE);

AnimatorSet scaleAnim = new AnimatorSet();
scaleAnim.setDuration(1000);
scaleAnim.play(scaleX).with(scaleY);

scaleAnim.start();
Run Code Online (Sandbox Code Playgroud)

  • 最后应该调用 `scaleAnim.start();`。`setRepeatCount` 和 `setRepeatMode` 是 `ObjectAnimator` 的方法,而不是 `AnimatorSet`。 (5认同)