▼Androidメモ▼
トゥイーンアニメーション


トゥイーンアニメーションを利用するプログラムを作成する。


リソース
「res/drawable-nodpi」に「face0.gif」を配置。

face0.gif

「res/anim」に「spin.xml」を配置。
spin.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false">
    <scale 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0" 
        android:toXScale="1.4" 
        android:fromYScale="1.0" 
        android:toYScale="0.6" 
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set 
        android:interpolator="@android:anim/accelerate_interpolator"
        android:startOffset="700">
        <scale
            android:fromXScale="1.4" 
            android:toXScale="0.0"
                android:fromYScale="0.6"
            android:toYScale="0.0" 
            android:pivotX="50%" 
            android:pivotY="50%" 
            android:duration="400" />
        <rotate 
            android:fromDegrees="0" 
            android:toDegrees="-45"
            android:toYScale="0.0" 
            android:pivotX="50%" 
            android:pivotY="50%"
            android:duration="400" />
    </set>
</set>

アニメーションタグ
アニメーションタグ 説明
alpha 透過率
scale スケール
translate

位置

rotate 回転
set 複数のアニメーションの合成

アニメーションタグの属性
アニメーションタグ アニメーションタグの属性 説明
共通 duration アニメの動作時間(ミリ秒)
startOffset アニメの開始時間(ミリ秒)
fillBefore

アニメ終了時に状態を元に戻す(true)

fillAfter アニメ終了時に状態をそのまま(true)
fillEnabled fillAfterを有効(true)
repeatCount リピート回数(-1:無限)
repeatMode リピート時に逆動作するか(restart/revers)
zAdjustment Z軸調整による重ね順(normal/top/bottom)
interpolator インターポレータ(アニメ変化の種別)
alpha fromAlpha アニメ開始時の透過率(0.0〜1.0)
toAlpha アニメ終了時の透過率(0.0〜1.0)
scale fromXScale アニメ開始時のXスケール(1.0が元サイズ)
toXScale アニメ終了時のXスケール(1.0が元サイズ)
fromYScale アニメ開始時のYスケール(1.0が元サイズ)
toYScale アニメ終了時のYスケール(1.0が元サイズ)
pivotX X原点(ドット、%:0%が左端、100%が右端)
pivotY Y原点(ドット、%:0%が上端、100%が下端)
translate fromXDelta アニメ開始時のX座標(ドット、%:相対位置、%p親ビュー相対位置)
toXDelta アニメ終了時のX座標(ドット、%:相対位置、%p親ビュー相対位置)
fromYDelta アニメ開始時のY座標(ドット、%:相対位置、%p親ビュー相対位置)
toYDelta アニメ終了時のY座標(ドット、%:相対位置、%p親ビュー相対位置)
rotate fromXDegree アニメ開始時の回転角度(-360〜360度)
toDegree アニメ終了時の回転角度(-360〜360度)
pivotX X原点(ドット、%:0%が左端、100%が右端)
pivotY Y原点(ドット、%:0%が上端、100%が下端)

インターポレータ
インターポレータ 説明
@android:anim/accelerate_decelerate_interpolator 徐々に遅く
@android:anim/accelerate_interpolator 徐々に速く
@android:anim/anticipate_interpolator 反対方向へ少し移動→目的地
@android:anim/anticipate_overshoot_interpolator 反対方向へ少し移動→目的地→少し通り過ぎる→目的地
@android:anim/bounce_interpolator 目的地→少し通り過ぎる→バウンドしながら目的地
@android:anim/cycle_interpolator 目的地→開始位置→反対方向→開始位置→…
@android:anim/decelerate_interpolator 急に遅く
@android:anim/linear_interpolator 等速
@android:anim/overshoot_interpolator 目的地→少し通り過ぎる→目的地

ソースコード
TweenAnimationEx.java
package net.npaka.tweenanimationex;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ImageView;

//トゥイーンアニメーション
public class TweenAnimationEx extends Activity {
    private final static int WC=LinearLayout.LayoutParams.WRAP_CONTENT;
    private ImageView imageView;
    
    //初期化
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //レイアウトの生成
        LinearLayout layout=new LinearLayout(this);
        layout.setBackgroundColor(Color.rgb(255,255,255));
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout); 
        
        //イメージビューの生成
        imageView=new ImageView(this);
        imageView.setBackgroundResource(R.drawable.face0);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(WC,WC));
        layout.addView(imageView);
    }

    //タッチ時に呼ばれる
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
            //トゥイーンアニメーション適用
            Animation animation=
                AnimationUtils.loadAnimation(this,R.anim.spin);
            imageView.startAnimation(animation);
            return true;
        }
        return super.onTouchEvent(event);
    }
}


−戻る−