Android中Activity多页滑动切换效果(使用ViewFlipper)

效果:



布局:


<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);
	}
	

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值