LoginSignup
12
14

More than 5 years have passed since last update.

減速せずに回転し続けるアニメーション

Posted at

以下のアニメーションでも回転し続けるが
1回のアニメーション終了前に、減速してからのリピートになるので
綺麗に同じスピードで回転し続けない

rotate_repleat.xml
<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360"
    android:fromDegrees="0"
    android:repeatCount="-1"
    >
</rotate>

ということで
上記のアニメーションのInterpolatorにLinearInterpolatorをセットすることで
減速のアニメーションを消しました

RotateAnimation animation = (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.rotate_repeat);
animation.setInterpolator(new LinearInterpolator());
findViewById(R.id.hoge).startAnimation(animation);

アニメーションxmlのinterpolatorにlinearを入れても同じ結果になりますが
この書き方だとAPIレベル10以下には対応していません

rotate_repleat.xml
<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/linear"
    android:duration="1000"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360"
    android:fromDegrees="0"
    android:repeatCount="-1"
    >
</rotate>

どうしてもxmlで対応する場合は
別のanimation.xmlにlinearInterpolatorを定義して

interpolator.xml
<?xml version="1.0" encoding="utf-8"?>
<linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android"/>

それを参照するとうまく行きます

rotate_repleat.xml
<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@anim/interpolator"
    android:duration="1000"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360"
    android:fromDegrees="0"
    android:repeatCount="-1"
    >
</rotate>
12
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14