【问题标题】:Rotation animation, slow it down旋转动画,放慢速度
【发布时间】:2014-03-14 15:13:02
【问题描述】:

我需要减慢图像视图上的旋转动画:它开始得更快,然后它应该“减速”直到动画结束(然后旋转停止)。我写了这个:

<?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)。怎么了?

【问题讨论】:

    标签: android animation


    【解决方案1】:

    简单地使用

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

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

    android:repeatCount="1"
    

    因为默认是0。

    【讨论】:

    • zealdeveloper 告诉我同样的事情,但效果不一样
    【解决方案2】:

    试试这个:

    <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 以便获得开始时更快并逐渐减慢的效果。

    【讨论】:

    • 我也尝试过使用该插值器,但它不起作用。我不明白为什么永远不会调用 onAnimationRepeat
    • 你可以尝试将repeatCount设置为无限
    • 我不需要重复到无限动画
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2011-10-19
    • 2015-10-07
    • 2021-02-05
    相关资源
    最近更新 更多