最終更新:
moonlight_aska 2015年11月29日(日) 00:15:10履歴
アニメーションフレームワークは, 以下の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/rotate.xml
- interpolatorは, アニメーション動作の変化を指定する. 例では, decelerate_interpolator(減速).
- propertyNameは, アニメーションするオブジェクトのプロパティ名を指定する. 例では, Viewのrotation.
- 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="rotation"
android:duration="2000"
android:valueFrom="0"
android:valueTo="90"
/>
</set>
- Animation02.java
- findViewByIdメソッドで, 指定したリソースインデックスのIDに対応したイメージビューのインスタンスを取得する.
- AnimatorInflater#loadAnimatorメソッドで, アニメーション設定ファイル(rotate.xml)を読み込み, AnimatorSetを作成する.
- AnimatorSet#setTargetメソッドで, ターゲットのオブジェクトを設定する.
- AnimatorSet#startメソッドで, アニメーションを開始する.
package com.moonlight_aska.android.animation02;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class Animation02 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.rotate);
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/rotate.xml
- RotateAnimationクラスで指定する属性
android:fromDegrees | アニメーション開始時の画面要素の角度 |
andorid:toDegrees | アニメーション終了時の画面要素の角度 |
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"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:duration="2000"
/>
- Animation02.java
- findViewByIdメソッドで, 指定したリソースインデックスのIDに対応したイメージビューのインスタンスを取得する.
- AnimationUtils#loadAnimationメソッドでアニメーション設定ファイル(rotate.xml)を読み込み, Animationを作成する.
- ImageView#startAnimationメソッドでアニメーションを開始する.
package com.moonlight_aska.android.animation02;
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class Animation02 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.rotate);
img.startAnimation(anim);
}
}
このページへのコメント
Androidのアニメーションを調べていてたどりつきました。
ありがとうございます
ではでは
http://www.mocjax.com/mocjax/nte_top.php