安卓动画不重复播放

87

我正在尝试制作一个简单的动画,它会重复几次(或者无限循环)。
看起来 android:repeatCount 不起作用!
这是我的动画资源,位于 /res/anim/first_animation.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:repeatCount="infinite"
    >
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.2"
        android:toYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false" />
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:startOffset="500"
        android:duration="500"
        android:fromXScale="1.2"
        android:fromYScale="1.2"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false" />
</set>

首先,它应该在500毫秒内将图像从1.0缩放到1.2的大小。
然后在500毫秒内将其缩放回1.0。
这是我如何使用它的:

Animation firstAnimation = AnimationUtils.loadAnimation(this, R.anim.first_animation);
imgView.startAnimation(firstAnimation);

它完成一次循环,然后停止。
它会缩放,然后再缩小,最后停止。

我该如何让其按照意图工作?


你的Java代码中,imgView是什么? - clifgray
23个回答

0
我在开发一个向后兼容的应用程序时遇到了这个问题,真是让人沮丧!最终,我编写了一个很好的解决方案类,可以从 onCreate 中调用,并将任何动画资源启动为无限循环。 这个类叫做 AnimationLooper,可以在这里找到:https://gist.github.com/2018678

0

我使用线程解决了这个问题。

Button btn = (Button) findViewById(R.id.buttonpush);
    final TextView textview = (TextView) findViewById(R.id.hello);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textview.setText("...................");
            final Animation animationtest = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left);
            animationtest.setDuration(1000);

            final Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                public void run() {
                    handler.postDelayed(this, 1500);
                    textview.startAnimation(animationtest);
                }
            };
            handler.postDelayed(runnable, 500); // start
            handler.removeCallbacks(runnable); //STOP Timer

        }
    });

-1
尝试将代码添加到循环线程或while/for语句中。

嗯,我找到的唯一解决方案是避免使用设置标签。 - Pavel Chernov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接