android之ViewFlipper滑屏切换效果

openkk 13年前

设置了三个页面,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>  <AbsoluteLayout xmlns: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">   <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:orientation="vertical" android:layout_width="fill_parent"     android:layout_height="fill_parent">     <TextView android:text="第 1 页"      android:textSize="35dp"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_x="115dp"      android:layout_y="20dp"/>    </AbsoluteLayout>    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="fill_parent"     android:layout_height="fill_parent">     <TextView android:text="第 2 页"      android:textSize="35dp"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_x="120dp"      android:layout_y="20dp"/>    </AbsoluteLayout>    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="fill_parent"     android:layout_height="fill_parent">     <TextView android: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>   <ImageButton android: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>   <ImageButton android: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 接口,具体如下:
import android.app.Activity;  import android.os.Bundle;  import android.view.GestureDetector;  import android.view.GestureDetector.OnGestureListener;  import android.view.MotionEvent;  import android.view.View;  import android.view.View.OnClickListener;  import android.view.View.OnTouchListener;  import android.view.animation.AnimationUtils;  import android.widget.ImageButton;  import android.widget.ViewFlipper;    /**   * ViewFlipperTest.java   * @author Cloay   * 2011-6-24   */  public class ViewFlipperTest 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.zd);      pre1Button = (ImageButton)findViewById(R.id.preButton1);    pre1Button.setOnTouchListener(new OnTouchListener(){       @Override     public boolean onTouch(View v, MotionEvent event) {      // TODO Auto-generated method stub      if(event.getAction()==MotionEvent.ACTION_DOWN){ //按钮按下背景图片       pre1Button.setBackgroundResource(R.drawable.pre_button1);      }      //按钮up后设置背景图片,并滑动到前一页面      else if(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();      }      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(TestFlip.this, R.anim.push_left_in));       flipper.setOutAnimation(AnimationUtils.loadAnimation(TestFlip.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;   }  }
测试时,鼠标点击模拟器快速移动松开(就是在屏幕上模拟手指滑动),结果如下: android之ViewFlipper滑屏切换效果 android之ViewFlipper滑屏切换效果

转自:http://blog.csdn.net/shang_515/article/details/6782133