ViewFlipper实现界面的滑动切换

近期,看到一些代码中实现了触屏滑动界面的效果,挺好奇的,就研究了一下android的这块的代码,之后就拼拼凑凑的写出了下面的代码。

首先,要实现滑动效果,需要继承一个android的接口OnGestureListener,接着在Activity中需要实现其接口,就可以完成实现滑动界面的效果了。

下述的push_right_in.xml,push_right_out.xml,push_left_in.xml和push_left_out.xml的代码见http://blog.csdn.net/faith_boys/article/details/8713357,

具体代码如下:

public class MainActivity extends Activity implements OnGestureListener {
	private ViewFlipper flipper;
	private GestureDetector detector;
	private ImageButton pre1Button;
	private ImageButton next1Button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		pre1Button = (ImageButton) findViewById(R.id.preButton1);
		pre1Button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 此处实现点击按钮切换界面的效果
				if (event.getAction() == MotionEvent.ACTION_DOWN) { 
					pre1Button.setBackgroundResource(R.drawable.pre_button1);
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					pre1Button.setBackgroundResource(R.drawable.pre_button);
					flipper.setInAnimation(AnimationUtils.loadAnimation(
							MainActivity.this, R.anim.push_right_in));
					flipper.setOutAnimation(AnimationUtils.loadAnimation(
							MainActivity.this, R.anim.push_right_out));
					flipper.showPrevious();
				}
				return false;
			}
		});

		next1Button = (ImageButton) findViewById(R.id.nextButton1);
		next1Button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					next1Button.setBackgroundResource(R.drawable.next_button1);
				}
				// 按钮up后设置背景图片,并滑动到后一页面
				else if (event.getAction() == MotionEvent.ACTION_UP) {
					next1Button.setBackgroundResource(R.drawable.next_button);
					flipper.setInAnimation(AnimationUtils.loadAnimation(
							MainActivity.this, R.anim.push_left_in));
					flipper.setOutAnimation(AnimationUtils.loadAnimation(
							MainActivity.this, R.anim.push_left_out));
					flipper.showNext();
				}
				return false;
			}
		});

		detector = new GestureDetector(this);
		flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper);
	}

	public boolean onDoubleTap(MotionEvent e) {

		if (flipper.isFlipping()) {
			flipper.stopFlipping();
		} else {
			flipper.startFlipping();
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	//实现触屏滑动,切换界面效果
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// 用户按下屏幕,快速移动后松开(就是在屏幕上滑动)
		// e1:第一个ACTION_DOWN事件(手指按下的那一点)
		// e2:最后一个ACTION_MOVE事件 (手指松开的那一点)
		// velocityX:手指在x轴移动的速度 单位:像素/秒
		// velocityY:手指在y轴移动的速度 单位:像素/秒
		if (e1.getX() - e2.getX() > 60) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));
			this.flipper.showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -60) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));
			this.flipper.showPrevious();
			return true;
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#338274"
    android:orientation="vertical" >

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

        <AbsoluteLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_x="115dp"
                android:layout_y="20dp"
                android:text="第 1 页"
                android:textSize="35dp" />
        </AbsoluteLayout>

        <AbsoluteLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_x="120dp"
                android:layout_y="20dp"
                android:text="第 2 页"
                android:textSize="35dp" />
        </AbsoluteLayout>

        <AbsoluteLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_x="120dp"
                android:layout_y="20dp"
                android:text="第 3 页"
                android:textSize="35dp" />
        </AbsoluteLayout>
    </ViewFlipper>

    <ImageButton
        android:id="@+id/preButton1"
        android:layout_width="35dp"
        android:layout_height="40dp"
        android:layout_x="101dp"
        android:layout_y="404dp"
        android:background="@drawable/pre_button" >
    </ImageButton>

    <ImageButton
        android:id="@+id/nextButton1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_x="182dp"
        android:layout_y="405dp"
        android:background="@drawable/next_button" >
    </ImageButton>

</AbsoluteLayout>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值