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

时间:2015-07-19 08:38:27

标签: android animation imageview zoom scale

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


            }
        });


    }

2 个答案:

答案 0 :(得分:3)

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

ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 1.1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 1.1f);

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

scaleAnim.setRepeatCount(ObjectAnimator.INFINITE);
scaleAnim.setRepeatMode(ObjectAnimator.RESTART/REVERSE...);

答案 1 :(得分:3)

顺序查看图像视图

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

compile 'org.apache.flink:flink-java:1.0.0-hadoop1'