パラパラ漫画のようなアニメーションは、画像ファイルでanimation-listを作り、AnimationDrawableクラスを使って実装します。
リソースをsetした時点で1枚目の画像が表示されるので、たとえば何かのトリガーで突然表示させたい場合などは、1枚目の画像を透明にするか、サンプルコードのようにトリガーが引かれてからsetBackgroundResource()するといいです。
なお、AnimationDrawableのstart()は、アニメーションが完了する前に復帰してきます。
アニメーション画像ファイル
res/drawable/anim1.png
res/drawable/anim2.png
res/drawable/anim3.png
res/drawable/anim4.png
res/drawable/anim5.png
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
public class MyClass extends Activity{
public void startAnimation(){
ImageView animView = (ImageView)findViewById(R.id.animview);
animView.setBackgroundResource(R.anim.myAnim);
AnimationDrawable anim = (AnimationDrawable)animView.getBackground();
anim.setOneShot(true); // 1回で終わる
if(anim.isRunning()) anim.stop(); // アニメーション中なら止める
anim.start();
}
}
res/anim/myAnim.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:duration="200" android:drawable="@drawable/anim1" /> <item android:duration="150" android:drawable="@drawable/anim2" /> <item android:duration="200" android:drawable="@drawable/anim3" /> <item android:duration="200" android:drawable="@drawable/anim4" /> <item android:duration="120" android:drawable="@drawable/anim5" /> </animation-list>
参考: Tweenアニメーション
参考: Tweenアニメーション for XML


コメント