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

<?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;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.zd);pre1Button = (ImageButton)findViewById(R.id.preButton1);pre1Button.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif(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(){@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif(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;       }  @Overridepublic boolean onTouchEvent(MotionEvent event) {return this.detector.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic 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;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}}

测试时,鼠标点击模拟器快速移动松开(就是在屏幕上模拟手指滑动),结果如下:

android之ViewFlipper滑屏切换效果_第1张图片android之ViewFlipper滑屏切换效果_第2张图片


说明:转载请注明出处!

更多相关文章

  1. 自定义控件:滑动开关按钮
  2. android设置背景图片,去除背景图片
  3. Android ToolBar Menu按钮的动态隐藏
  4. android在grid组件中加入添加删除图片按钮
  5. Android点击按钮隐藏或者打开软键盘
  6. 隐藏 video标签的下载按钮
  7. 控件_RadioGroup&&RadioButton(单选按钮)和Toast

随机推荐

  1. Android获取屏幕宽度高度并动态设置控件
  2. Android事件分发机制(一)
  3. Android(安卓)Service BroadcastReceiver
  4. 最近喜欢上ubuntu,给大家分享一下如何在ub
  5. 2012-06-13 16:50 Android限定EditText的
  6. 如何在一台计算机上安装多个 JDK 版本
  7. 二、Tiny4412开发板运行安卓系统
  8. Android(安卓)Intent原理分析
  9. Android(安卓)对返回按键点击次数的监听
  10. 蓝牙socket读取数据需读多次才读全