最終更新:
moonlight_aska 2015年11月29日(日) 00:04:07履歴
アニメーションフレームワークは, 以下の2種類が準備されている.
アニメーションの一種である拡大・縮小アニメーションを行うには, アニメータ(Animator)クラスあるいはビューアニメーション(view.Animation)クラスを利用する.

↓ 拡大

- Property Animation : Android 3.0 (API Level 11)
- View Animation : Android 1.0 (API Level 1)
アニメーションの一種である拡大・縮小アニメーションを行うには, アニメータ(Animator)クラスあるいはビューアニメーション(view.Animation)クラスを利用する.

↓ 拡大

- res/animator/scale.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>
- res/anim/scale.xml
- ScaleAnimationクラスで指定する属性
android:fromXScale | アニメーション開始時の水平方向の要素 |
andorid:fromYScale | アニメーション開始時の垂直方向の要素 |
android:toXScale | アニメーション終了時の水平方向の要素 |
android:toYScale | アニメーション終了時の垂直方向の要素 |
android:pivotXType | pivotXValueの設定方法 |
android:pivotYType | pivotYVakyeの設定方法 |
android:pivotX | サイズ変更時のX座標の基点 |
android:pivotY | サイズ変更時のY座標の基点 |
- Animationクラスで指定する属性
android:duration | アニメーションの動作時間(msec) |
android:fillAfter | true:アニメーション終了後にアニメーションの変換が適用 |
android:fillBefore | true:アニメーション開始前にアニメーションの変換が適用 |
android:fillEnabled | true: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);
}
}
コメントをかく