Android中使用ViewFlipper进行手势切换实例
本文实例讲述了Android中使用ViewFlipper进行手势切换的方法,分享给大家供大家参考。具体实现步骤如下:
首先在layout的xml文件中定义一个ViewFlipper:
<?xmlversion="1.0"encoding="utf-8"?> <ViewFlipperxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewFlipper" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF0000" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00FF00" /> </LinearLayout> </ViewFlipper>
声明一个新的GestureDetector,重写它的onFling()函数,在这个函数里面判断手势,这里为横向的拖动:
publicclassMyGestureDetectorextendsSimpleOnGestureListener { privatestaticfinalintSWIPE_MIN_DISTANCE=120; privatestaticfinalintSWIPE_MAX_OFF_PATH=250; privatestaticfinalintSWIPE_THRESHOLD_VELOCITY=200; @Override publicbooleanonFling(MotionEvente1,MotionEvente2, floatvelocityX,floatvelocityY) { try { if(Math.abs(e1.getY()-e2.getY())>SWIPE_MAX_OFF_PATH) { returnfalse; } 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(Exceptione) { } returnfalse; } }
注意在Activity中重写onTouchEvent()函数:
privateViewFlipperviewFlipper; privateAnimationslideLeftIn; privateAnimationslideLeftOut; privateAnimationslideRightIn; privateAnimationslideRightOut; privateGestureDetectorgestureDetector; @Override publicvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); viewFlipper=(ViewFlipper)findViewById(R.id.viewFlipper); 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); gestureDetector=newGestureDetector(newMyGestureDetector()); newView.OnTouchListener() { publicbooleanonTouch(Viewv,MotionEventevent) { if(gestureDetector.onTouchEvent(event)) { returntrue; } returnfalse; } }; } @Override publicbooleanonTouchEvent(MotionEventevent) { if(gestureDetector.onTouchEvent(event)) { returntrue; } else { returnfalse; } }
最后,在res文件夹下新建一个anim文件夹,用来存放view之间切换的动画文件:
1.slide_left_in.xml
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" /> </set>
2.slide_left_out.xml
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" /> </set>
3.slide_right_in.xml
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" /> </set>
4.slide_right_out.xml
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" /> </set>
希望本文所述对大家的Android程序设计有所帮助