设置了三个页面,布局文件如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent"android:background="@drawable/dbj">
- <ViewFlipperandroid:id="@+id/ViewFlipper"
- android:layout_width="fill_parent"android:layout_height="fill_parent">
- <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextViewandroid:text="第1页"
- android:textSize="35dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="115dp"
- android:layout_y="20dp"/>
- </AbsoluteLayout>
- <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextViewandroid:text="第2页"
- android:textSize="35dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="120dp"
- android:layout_y="20dp"/>
- </AbsoluteLayout>
- <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextViewandroid:text="第3页"
- android:textSize="35dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="120dp"
- android:layout_y="20dp"/>
- </AbsoluteLayout>
- </ViewFlipper>
- <ImageButtonandroid:layout_width="35dp"
- android:background="@drawable/pre_button"android:layout_height="40dp"
- android:id="@+id/preButton1"android:layout_x="101dp"
- android:layout_y="404dp">
- </ImageButton>
- <ImageButtonandroid:layout_width="40dp"
- android:background="@drawable/next_button"android:layout_height="40dp"
- android:id="@+id/nextButton1"android:layout_x="182dp"
- android:layout_y="405dp">
- </ImageButton>
- </AbsoluteLayout>
主程序实现了OnGestureListener 接口,具体如下:
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.GestureDetector;
- importandroid.view.GestureDetector.OnGestureListener;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.View.OnTouchListener;
- importandroid.view.animation.AnimationUtils;
- importandroid.widget.ImageButton;
- importandroid.widget.ViewFlipper;
- /**
- *ViewFlipperTest.java
- *@authorCloay
- *2011-6-24
- */
- publicclassViewFlipperTestextendsActivityimplementsOnGestureListener{
- privateViewFlipperflipper;
- privateGestureDetectordetector;
- privateImageButtonpre1Button;
- privateImageButtonnext1Button;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.zd);
- pre1Button=(ImageButton)findViewById(R.id.preButton1);
- pre1Button.setOnTouchListener(newOnTouchListener(){
- @Override
- publicbooleanonTouch(Viewv,MotionEventevent){
- //TODOAuto-generatedmethodstub
- if(event.getAction()==MotionEvent.ACTION_DOWN){//按钮按下背景图片
- pre1Button.setBackgroundResource(R.drawable.pre_button1);
- }
- //按钮up后设置背景图片,并滑动到前一页面
- elseif(event.getAction()==MotionEvent.ACTION_UP){
- pre1Button.setBackgroundResource(R.drawable.pre_button);
- flipper.setInAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_right_in));
- flipper.setOutAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_right_out));
- flipper.showPrevious();
- }
- returnfalse;
- }
- });
- next1Button=(ImageButton)findViewById(R.id.nextButton1);
- next1Button.setOnTouchListener(newOnTouchListener(){
- @Override
- publicbooleanonTouch(Viewv,MotionEventevent){
- //TODOAuto-generatedmethodstub
- if(event.getAction()==MotionEvent.ACTION_DOWN){
- next1Button.setBackgroundResource(R.drawable.next_button1);
- }
- //按钮up后设置背景图片,并滑动到后一页面
- elseif(event.getAction()==MotionEvent.ACTION_UP){
- next1Button.setBackgroundResource(R.drawable.next_button);
- flipper.setInAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_left_in));
- flipper.setOutAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_left_out));
- flipper.showNext();
- }
- returnfalse;
- }
- });
- detector=newGestureDetector(this);
- flipper=(ViewFlipper)this.findViewById(R.id.ViewFlipper);
- }
- publicbooleanonDoubleTap(MotionEvente){
- if(flipper.isFlipping()){
- flipper.stopFlipping();
- }else{
- flipper.startFlipping();
- }
- returntrue;
- }
- @Override
- publicbooleanonTouchEvent(MotionEventevent){
- returnthis.detector.onTouchEvent(event);
- }
- @Override
- publicbooleanonDown(MotionEvente){
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- @Override
- publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,floatvelocityY){
- //用户按下屏幕,快速移动后松开(就是在屏幕上滑动)
- //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();
- returntrue;
- }elseif(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();
- returntrue;
- }
- returnfalse;
- }
- @Override
- publicvoidonLongPress(MotionEvente){
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicbooleanonScroll(MotionEvente1,MotionEvente2,floatdistanceX,
- floatdistanceY){
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- @Override
- publicvoidonShowPress(MotionEvente){
- //TODOAuto-generatedmethodstub
- }
- @Override
- publicbooleanonSingleTapUp(MotionEvente){
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- }
测试时,鼠标点击模拟器快速移动松开(就是在屏幕上模拟手指滑动),结果如下: