2012年04月15日(日)
Tweened Animation
|上の文書に書かれている Tweened Animation を書いてみました。
Tweened Animation は画像変換を連続表示して実現したアニメーションみたいです。
作成
上の文書に書かれている XML を流用しました。
res/anim/animation.xml (アニメーションの定義。res/anim に置く)
ここでは、<scale>、<rotate> しか使ってませんが、<alpha>、<translate>というのもあるようです。
<?xml version="1.0" encoding="UTF-8"?> <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> <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/decelerate_interpolator"> <scale android:fromXScale="1.4" android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" android:fillBefore="false" /> <rotate android:fromDegrees="0" android:toDegrees="-45" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" /> </set> </set>
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/anim_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:contentDescription="@string/anim_view_description"/> <Button android:id="@+id/start_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start_button_label" android:onClick="onStartAnim"/> <Button android:id="@+id/stop_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop_button_label" android:onClick="onStopAnim"/> </LinearLayout>
res/values/string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">AnimSample</string> <string name="start_button_label">Start</string> <string name="stop_button_label">Stop</string> <string name="anim_view_description">Anime View</string> </resources>
src/.../AnimSampleActivity.java
package com.example.animsample; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; public class AnimSampleActivity extends Activity { private ImageView animView; private Animation animation; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); animView = (ImageView)findViewById(R.id.anim_view); animation = AnimationUtils.loadAnimation(this, R.anim.animation); } public void onStartAnim(View v) { animView.startAnimation(animation); } public void onStopAnim(View v) { animView.clearAnimation(); } }
コメント