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

效果:

Android中Activity多页滑动切换效果(使用ViewFlipper)_第1张图片


布局:




    

        
        

            
        

        
        

            
        
    

    



定义动画效果:

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

}


你可能感兴趣的:(Android,移动开发,Android)