カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

Androidでアニメーション

ImageViewをアニメーションさせてみます。
まずはコードでアニメーションを設定する方法を紹介します。

以下のようなImageViewがあったとします。

<ImageView id="@+id/img"
  android:src="@drawable/icon"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

ImageViewオブジェクトを生成

ImageView img = (ImageView)findViewById(R.id.img);

移動するアニメーション【TranslateAnimation】

//TranslateAnimation(float fromX, float toX, float fromY, float toY)
TranslateAnimation translate = new TranslateAnimation(0, 10, 0, 0);
//動作時間を設定(単位ms)
translate.setDuration(1000);
//繰り返す回数を設定(1度でよい場合は設定しない)
translate.setInterpolator(new CycleInterpolator(3));
 
//アニメーションを開始
img.startAnimation(translate);

続きを読む…»

17,927 views