旋转动画,慢下来

时间:2014-03-14 15:13:03

标签: android animation

我需要在imageview上减慢一个旋转动画:它开始得更快,然后它应该减速"直到动画结束(然后旋转停止)。我写了这个:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
    android:duration="100"
    android:fromDegrees="0"
    android:interpolator="@android:anim/cycle_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="30"
    android:repeatMode="restart"
    android:toDegrees="360" />

</set>

然后将侦听器添加到动画中:

Animation rotate = AnimationUtils
            .loadAnimation(activity, R.anim.rotate);
    ImageView logo = (ImageView) SplashScreen.activity
            .findViewById(R.id.logo);


    rotate.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Intent intent = new Intent(SplashScreen.this,
                    LoginActivity.class);
            SplashScreen.this.startActivity(intent);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            if(animation.getRepeatCount() == 5) {
                animation.setDuration(200);
            } else if (animation.getRepeatCount() == 10) {
                Log.i("ANIM", "10");
                animation.setDuration(5000);
            } else if (animation.getRepeatCount() == 15) {
                animation.setDuration(800);
            } else if (animation.getRepeatCount() == 20) {
                animation.setDuration(1600);
            } else if (animation.getRepeatCount() == 25) {
                animation.setDuration(2000);
            } 

        }

    });

    logo.setAnimation(rotate);
    logo.startAnimation(rotate);

但动画总是具有相同的速度(代码永远不会进入onAnimationRepeat)。怎么了?

2 个答案:

答案 0 :(得分:2)

只需使用

android:interpolator="@android:anim/decelerate_interpolator"

在动画xml文件中。  检查此链接以了解其他插补器http://developer.android.com/reference/android/view/animation/package-summary.html 还添加

android:repeatCount="1"

因为默认值为0.

答案 1 :(得分:0)

试试这个:

<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
    android:duration="100"
    android:fromDegrees="0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="30"
    android:repeatMode="restart"
    android:toDegrees="360" />

</set>

将插补器cycle_interpolator更改为decelerate_interpolator,以便在开始时获得更快的效果并逐渐减慢。