在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:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/viewflipper1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<include
android:id="@+id/layout1"
layout="@layout/view_1" />
<include
android:id="@+id/layout2"
layout="@layout/view_2" />
<include
android:id="@+id/layout3"
layout="@layout/view_3" />
</ViewFlipper>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一个" />
</LinearLayout>
view_1.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="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo_1" >
</ImageView>
</LinearLayout>
view_2.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="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo_3" >
</ImageView>
</LinearLayout>
view_3.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="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/photo_2" >
</ImageView>
</LinearLayout>
在anim文件夹下定义动画文件
fipper_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
fipper_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="100%p" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
ActivityMain
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;
public class ActivityMain extends Activity {
//定义ViewFlipper对象
private ViewFlipper mViewFlipper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取ViewFlipper对象
mViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper1);
//设置动画
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.flipper_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.flipper_out));
mViewFlipper.setFlipInterval(2000);//设置View之间切换的时间间隔
//下一个按钮
Button btn = (Button)this.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//用来判断View切换是否正在进行
if(mViewFlipper.isFlipping()){
//下一个
mViewFlipper.showNext();
}else{
//使用上面设置的时间间隔来开始切换所有的View,切换会循环进行
mViewFlipper.startFlipping();
}
}
});
}
}
ViewFipper可以添加2个或者两个以上的View,并在同一个时间就有一个view会显示。如果有需要可以设置每个view在固定时候会切换。并且可以设置切换方式的自定义动画效果。