2011年12月10日土曜日

ViewFlipperにアニメーション設定



package com.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class AndroidTestActivity extends Activity {

 private ViewFlipper viewFlipper;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
 }

 public void MyOnClickButtonNext(View v) {
  viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_left));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_right));
        viewFlipper.showNext();
 }

 public void MyOnClickButtonPrevious(View v) {
  viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_right));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_left));
        viewFlipper.showPrevious();
 }
}

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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="メイン画面" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="MyOnClickButtonPrevious"
            android:text="戻る" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="MyOnClickButtonNext"
            android:text="次へ" />
    </LinearLayout>

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <include
            android:id="@+id/firstlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            layout="@layout/first" />

        <include
            android:id="@+id/secondlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            layout="@layout/second" />

        <include
            android:id="@+id/thirdlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            layout="@layout/third" />
    </ViewFlipper>

</LinearLayout>

res/layout/first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一画面" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/vermeer02" android:layout_gravity="center_horizontal"/>

</LinearLayout>

res/anim/in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:fromXDelta="-100%p"
    android:toXDelta="0"
    android:duration="500"/>
</set>

res/anim/in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set 
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:fromXDelta="100%p"
    android:toXDelta="0"
    android:duration="500"/>
</set>

res/anim/out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:fromXDelta="0"
    android:toXDelta="-100%p"
    android:duration="500"/>
</set>

res/anim/out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set
  xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:fromXDelta="0"
    android:toXDelta="100%p"
    android:duration="500"/>
</set>

関連記事

0 件のコメント:

コメントを投稿