效果:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 第一页 -->
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A020F0"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is first page" />
</LinearLayout>
<!-- 第二页 -->
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4876FF"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is second page" />
</LinearLayout>
</ViewFlipper>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
定义动画效果:
package com.cn.zzti.ly.util;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
/*
* 定义动画效果
*/
public class AnimationUtil {
public static Animation inLeftToRight() {
//定义平移动画
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
translateAnimation.setDuration(300); //设置持续时间
return translateAnimation;
}
public static Animation outLeftToRight() {
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
translateAnimation.setDuration(300);
return translateAnimation;
}
public static Animation inRightToLeft() {
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
translateAnimation.setDuration(300);
return translateAnimation;
}
public static Animation outRightToLeft() {
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
translateAnimation.setDuration(300);
return translateAnimation;
}
}
Activity:
package com.cn.zzti.ly.ui;
import com.cn.zzti.ly.util.AnimationUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private ViewFlipper viewFlipper;
private float startX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
startX = event.getX();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
float endX = event.getX();
if (endX > startX + 50)
{
//设置进入和出去的平移动画效果
viewFlipper.setInAnimation(AnimationUtil.inLeftToRight());
viewFlipper.setOutAnimation(AnimationUtil.outLeftToRight());
viewFlipper.showNext(); //显示下一页
}
else if (endX < startX - 50)
{
viewFlipper.setInAnimation(AnimationUtil.inRightToLeft());
viewFlipper.setOutAnimation(AnimationUtil.outRightToLeft());
viewFlipper.showPrevious(); //显示前一页
}
return true;
}
return super.onTouchEvent(event);
}
}