最終更新: moonlight_aska 2015年12月02日(水) 23:21:14履歴
Property Animation(API Level 11)において, シンプルなアニメーションを繰り返し実行するには, objectAnimator/animatorタグのrepeatCount属性とrepeatMode属性を利用する.
しかし, 複数のプロパティを組み合わせたアニメーションの場合, 全体を通したアニメーションの繰り返しはrepeatCount/repeatMode属性では記述できない. しかし, 単純な繰り返しであれば, アニメーションの終了イベント発生時に再度アニメーションを開始することで実現可能である.
しかし, 複数のプロパティを組み合わせたアニメーションの場合, 全体を通したアニメーションの繰り返しはrepeatCount/repeatMode属性では記述できない. しかし, 単純な繰り返しであれば, アニメーションの終了イベント発生時に再度アニメーションを開始することで実現可能である.
「回転アニメーションを行う」のProperty Animationを参照.
- res/animator/repeat.xml
- リピート条件を設定する属性
android:repeatCount | 繰り返し実行回数. -1を指定すると無限に繰り返し. |
android:repeatMode | "restart"は最初から繰り返す. "reverse"は逆方向に繰り返す. |
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
android:interpolator="@android:anim/decelerate_interpolator"
android:propertyName="rotation"
android:duration="5000"
android:valueFrom="0"
android:valueTo="360"
android:repeatCount="10"
android:repeatMode="reverse"
/>
</set>
- Animation01.java
- AnimatorInflater#loadAnimatorメソッドで, アニメーション設定ファイル(mix.xml)を読み込み, AnimatorSetを作成する.
- AnimatorSet#addListenerメソッドで, AnimatorListenerAdapterクラスを設定する.
- onAnimationEndメソッドをオーバーライドし, リピート回数内の場合にはAnimationSet#startメソッドでアニメーションを再生する.
package com.moonlight_aska.android.work.animation01;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int REPEAT_COUNT = 10;
private AnimatorSet mSet = null;
private int mCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.robot);
mSet = (AnimatorSet)AnimatorInflater.loadAnimator(this, R.animator.mix);
mSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// リピート回数内の場合は再度アニメーション再生
if (++mCount < REPEAT_COUNT) {
mSet.start();
}
}
});
mSet.setTarget(img);
mSet.start();
}
}
コメントをかく