2010年7月6日火曜日

AnimationDrawable

ロード中の画像みたいにクルクル回したい場合はAnimationDrawableを使う。
(ゲームのスプライトアニメーション的なやつ)

【アンドロイドSDKインストールディレクトリ】\platforms\android-4\data\res\drawable\ic_popup_sync.xml
あたりを参考に。

ic_popup_sync.xml
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/ic_popup_sync_1" android:duration="200" />
<item android:drawable="@drawable/ic_popup_sync_2" android:duration="200" />
<item android:drawable="@drawable/ic_popup_sync_3" android:duration="200" />
<item android:drawable="@drawable/ic_popup_sync_4" android:duration="200" />
<item android:drawable="@drawable/ic_popup_sync_5" android:duration="200" />
<item android:drawable="@drawable/ic_popup_sync_6" android:duration="200" />
</animation-list>

android:durationを小さくすれば早く画像が切り替わる。


Activity抜粋

AnimationDrawable d;
private ImageView imgView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgView = (ImageView) findViewById(R.id.ImageView01);
d = (AnimationDrawable) getResources().getDrawable(android.R.drawable.ic_popup_sync);
imgView.setImageDrawable(d);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
d.start();
}
}
onWindowFocusChangedとかでstartするのがポイント。
onCreateとかonResumeでstartさせてもアニメーションしてくれない。

詳しくは

0 件のコメント:

コメントを投稿