C/C++プログラマの管理者が, Androidプログラムにチャレンジ. AndroidプログラミングのTipsをメモっていく予定です.

アニメーションフレームワークは, 以下の2種類が準備されている.
  • Property Animation : Android 3.0 (API Level 11)
  Viewに限らず任意のオブジェクト(View, Drawable, Fragument, etc)に対してアニメーションを実行できる.
  • View Animation : Android 1.0 (API Level 1)
  Viewに対してのみアニメーションを実行できる.

アニメーションの一種である拡大・縮小アニメーションを行うには, アニメータ(Animator)クラスあるいはビューアニメーション(view.Animation)クラスを利用する.


            拡大


Property Animation (API Level 11)

  • res/animator/scale.xml
アニメーションの設定を行うXMLファイルである.
    • interpolatorは, アニメーション動作の変化を指定する. 例では, decelerate_interpolator(減速).
    • propertyNameは, アニメーションするオブジェクトのプロパティ名を指定する. 例では, ViewのscaleX, Y.
    • durationは, アニメーションの動作時間(ms)を指定する.
    • valueFromとvalueToは, 指定したpropertyのアニメーション開始/終了時の値を指定する. 例では, 拡大率.
<?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="scaleX"
    android:duration="2000"
    android:valueFrom="1.0"
    android:valueTo="2.0"
    />
  <objectAnimator
    android:interpolator="@android:anim/decelerate_interpolator"
    android:propertyName="scaleY"
    android:duration="2000"
    android:valueFrom="1.0"
    android:valueTo="2.0"
    />
</set>
  • Animation01.java
    • findViewByIdメソッドで, 指定したリソースインデックスのIDに対応したイメージビューのインスタンスを取得する.
    • AnimatorInflater#loadAnimatorメソッドで, アニメーション設定ファイル(scale.xml)を読み込み, AnimatorSetを作成する.
    • AnimatorSet#setTargetメソッドで, ターゲットのオブジェクトを設定する.
    • AnimatorSet#startメソッドで, アニメーションを開始する.
package com.moonlight_aska.android.animation01;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class Animation01 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView img = (ImageView)findViewById(R.id.robot);
    AnimatorSet set = (AnimatorSet)AnimatorInflater.loadAnimator(this, R.animator.scale);
    set.setTarget(img);
    set.start();
  }
}
  • res/layout/main.xml
    • 画像ファイル(robot.png)は, res/drawableフォルダの下に置く.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<ImageView
  android:id="@+id/robot"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:src="@drawable/robot"
  />
</RelativeLayout>

View Animation (API Level 1)

  • res/anim/scale.xml
アニメーションの設定を行うXMLファイルである.
    • ScaleAnimationクラスで指定する属性
android:fromXScaleアニメーション開始時の水平方向の要素
andorid:fromYScaleアニメーション開始時の垂直方向の要素
android:toXScaleアニメーション終了時の水平方向の要素
android:toYScaleアニメーション終了時の垂直方向の要素
android:pivotXTypepivotXValueの設定方法
android:pivotYTypepivotYVakyeの設定方法
android:pivotXサイズ変更時のX座標の基点
android:pivotYサイズ変更時のY座標の基点
    • Animationクラスで指定する属性
android:durationアニメーションの動作時間(msec)
android:fillAftertrue:アニメーション終了後にアニメーションの変換が適用
android:fillBeforetrue:アニメーション開始前にアニメーションの変換が適用
android:fillEnabledtrue:fillAfterが適用
android:repeatCountアニメーションの繰り返し回数
android:repeatModeアニメーションが終了した場合の振る舞い
android:startOffsetアニメーションが開始する前に置く時間(msec)
android:zAdjustmentアニメーション実行中のコンテンツについてZ軸方向の調整
<?xml version="1.0" encoding="utf-8"?>
<scalexmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/decelerate_interpolator"
  android:fromXScale="1.0"
  android:toXScale="2.0"
  android:fromYScale="1.0"
  android:toYScale="2.0"
  android:pivotX="50%"
  android:pivotY="50%"
  android:fillAfter="true"
  android:duration="2000"
  />
  • Animation01.java
    • findViewByIdメソッドで, 指定したリソースインデックスのIDに対応したイメージビューのインスタンスを取得する.
    • AnimationUtils#loadAnimationメソッドでアニメーション設定ファイル(scale.xml)を読み込み, Animationを作成する.
    • ImageView#startAnimationメソッドでアニメーションを開始する.
package com.moonlight_aska.android.animation01;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class Animation01 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView img = (ImageView)findViewById(R.id.robot);
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);
    img.startAnimation(anim);
  }
}



コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu


逆引き(基礎編)

画面表示/操作(49)

フラグメント(1)

逆引き(応用編)

Firebase(2)

AD



管理人/副管理人のみ編集できます