运行效果:
package com.iaiai;import android.app.Activity;import android.os.Bundle;import android.view.GestureDetector;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.MotionEvent;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ViewFlipper;/** * * <p> * Title: MainActivity.java * </p> * <p> * E-Mail: [email protected] * </p> * <p> * QQ: 176291935 * </p> * <p> * Http: iaiai.iteye.com * </p> * <p> * Create time: 2011-10-24 * </p> * * @author 丸子 * @version 0.0.1 */public class MainActivity extends Activity { private ViewFlipper viewFlipper; private GestureDetector gestureDetector; private Animation slideLeftIn; private Animation slideLeftOut; private Animation slideRightIn; private Animation slideRightOut; private class GestureDetectorListener extends SimpleOnGestureListener { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SWIPE_MAX_OFF_PATH = 250; private static final int SWIPE_THRESHOLD_VELOCITY = 200; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { viewFlipper.setInAnimation(slideLeftIn); viewFlipper.setOutAnimation(slideLeftOut); viewFlipper.showNext(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { viewFlipper.setInAnimation(slideRightIn); viewFlipper.setOutAnimation(slideRightOut); viewFlipper.showPrevious(); } } catch (Exception e) { // nothing } return false; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in); slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out); slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in); slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out); viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper); gestureDetector = new GestureDetector(new GestureDetectorListener()); } @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); }}
1 楼 鱼骨头123 2011-11-07
你好,你的教程很好,还有个问题想问一下:
<LinearLayout>
<TextView></TextView> //把此行删掉才能运行成功,不删掉运行时模拟器会有程序崩溃
<ViewFlipper>
<LinearLayout></LinearLayout>
<ViewFlipper>
<LinearLayout>
<TextView></TextView> //把此行删掉才能运行成功,不删掉运行时模拟器会有程序崩溃
<ViewFlipper>
<LinearLayout></LinearLayout>
<ViewFlipper>
2 楼 鱼骨头123 2011-11-07
鱼骨头123 写道
你好,你的教程很好,还有个问题想问一下:
<LinearLayout>
<TextView></TextView> //把此行删掉才能运行成功,不删掉运行时模拟器会有程序崩溃
<ViewFlipper>
<LinearLayout></LinearLayout>
<ViewFlipper>
<LinearLayout>
<TextView></TextView> //把此行删掉才能运行成功,不删掉运行时模拟器会有程序崩溃
<ViewFlipper>
<LinearLayout></LinearLayout>
<ViewFlipper>
那我怎么实现部分页面滑动切换呢?