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

ImageView zoom in, zoom out infinite animation and onAnimationRepeat issue

我想为我的 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;
                }


            }
        });


    }

只需切换到 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();

按顺序查看图片动画

  • 初始化 i=0
  • 创建不同的动画对象
  • 创建图像视图 ID 数组
  • 在处理人员的帮助下决定您的时间段
  • 在 anim 文件夹中添加您的动画文件

final Animation animFirst = AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_expand);
final Animation animSecond = AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_expand);
final Animation animThird = AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_expand);


final int[] imageId = new int[]{R.id.step_1, R.id.step_2, R.id.step_3};
final List<Animation> anim = new ArrayList<>();
anim.add(animFirst);
anim.add(animSecond);
anim.add(animThird);


final Handler handler = new Handler();
handler.postDelayed(new Runnable() {


    public ImageView imageView;

    public void run() {
        if (i < imageId.length) {
           imageView= ((ImageView) mBinding.getRoot().findViewById(imageId[i]));
            imageView .startAnimation(anim.get(i));
            anim.get(i).setFillAfter(true);

            i++;
        } else {
            i = 0;
            anim.get(0).setFillAfter(false);
            anim.get(1).setFillAfter(false);
            anim.get(2).setFillAfter(false);

        }
        handler.postDelayed(this, 2000);
    }

}, 500);



}