設置了三個頁面,布局文件如下:
- <?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;
- }
- }
測試時,鼠標點擊模擬器快速移動松開(就是在屏幕上模擬手指滑動),結果如下: