Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例
本文实例讲述了Android使用ViewFlipper和GestrueDetector共同实现滑屏效果。分享给大家供大家参考,具体如下:
关于GestureDetector的相关知识,前面已经介绍过了,不懂的大家可以去了解一下。
1.main.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg" android:orientation="vertical"> <ViewFlipper android:id="@+id/viewFlipper1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:persistentDrawingCache="animation" android:flipInterval="1000" android:inAnimation="@anim/push_left_in" android:outAnimation="@anim/push_left_out" > <includeandroid:id="@+id/lay1"layout="@layout/layout1"/> <includeandroid:id="@+id/lay2"layout="@layout/layout2"/> </ViewFlipper> </LinearLayout>
注:layout1和layout2布局很简单,就是有一个textview和一个button,就不在这里写出了。其中包含了两个特效xml文件,放在res/anim下
1.push_left_in
<?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>
2push_left_out
<?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>
主类:
packagecom.wj.gesture; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.GestureDetector; importandroid.view.GestureDetector.OnDoubleTapListener; importandroid.view.GestureDetector.OnGestureListener; importandroid.view.MotionEvent; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.view.View.OnTouchListener; importandroid.view.animation.AccelerateInterpolator; importandroid.view.animation.Animation; importandroid.view.animation.TranslateAnimation; importandroid.widget.Button; importandroid.widget.TextView; importandroid.widget.Toast; importandroid.widget.ViewFlipper; publicclassGestureDetectorTestextendsActivityimplementsOnClickListener,OnTouchListener,OnGestureListener,OnDoubleTapListener{ GestureDetectorgestureDetector; privateButtonnext=null,up=null; privateViewFlipperflipper=null; privatestaticfinalintFLING_MIN_DISTANCE=100; privatestaticfinalintFLING_MIN_VELOCITY=200; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); initview(); setListener(); } privatevoidinitview(){ gestureDetector=newGestureDetector(this); next=(Button)this.findViewById(R.id.button1); up=(Button)this.findViewById(R.id.button2); next.setOnClickListener(this); up.setOnClickListener(this); flipper=(ViewFlipper)this.findViewById(R.id.viewFlipper1); flipper.setLongClickable(true); } privatevoidsetListener(){ flipper.setOnTouchListener(this); } @Override publicbooleanonTouch(Viewv,MotionEventevent){ //Toast.makeText(this,"ontouch",1000).show(); returngestureDetector.onTouchEvent(event); } @Override publicbooleanonDown(MotionEvente){ //TODOAuto-generatedmethodstub returnfalse; } @Override publicvoidonShowPress(MotionEvente){ //TODOAuto-generatedmethodstub } @Override publicbooleanonSingleTapUp(MotionEvente){ //TODOAuto-generatedmethodstub returnfalse; } @Override publicbooleanonScroll(MotionEvente1,MotionEvente2,floatdistanceX, floatdistanceY){ returnfalse; } @Override publicvoidonLongPress(MotionEvente){ //TODOAuto-generatedmethodstub } @Override publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX, floatvelocityY){ if(e1.getX()-e2.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){ flipper.setInAnimation(inFromRightAnimation()); flipper.setOutAnimation(outToLeftAnimation()); flipper.showNext(); }elseif(e2.getX()-e1.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){ flipper.setInAnimation(inFromLeftAnimation()); flipper.setOutAnimation(outToRightAnimation()); flipper.showPrevious(); } returnfalse; } protectedAnimationinFromRightAnimation(){ AnimationinFromRight=newTranslateAnimation( Animation.RELATIVE_TO_PARENT,+1.0f, Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,0.0f); inFromRight.setDuration(500); inFromRight.setInterpolator(newAccelerateInterpolator()); returninFromRight; } protectedAnimationoutToLeftAnimation(){ AnimationouttoLeft=newTranslateAnimation( Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,-1.0f, Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,0.0f); outtoLeft.setDuration(500); outtoLeft.setInterpolator(newAccelerateInterpolator()); returnouttoLeft; } protectedAnimationinFromLeftAnimation(){ AnimationinFromLeft=newTranslateAnimation( Animation.RELATIVE_TO_PARENT,-1.0f, Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,0.0f); inFromLeft.setDuration(500); inFromLeft.setInterpolator(newAccelerateInterpolator()); returninFromLeft; } protectedAnimationoutToRightAnimation(){ AnimationouttoRight=newTranslateAnimation( Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,+1.0f, Animation.RELATIVE_TO_PARENT,0.0f, Animation.RELATIVE_TO_PARENT,0.0f); outtoRight.setDuration(500); outtoRight.setInterpolator(newAccelerateInterpolator()); returnouttoRight; } @Override publicbooleanonSingleTapConfirmed(MotionEvente){ //TODOAuto-generatedmethodstub returnfalse; } @Override publicbooleanonDoubleTap(MotionEvente){ //TODOAuto-generatedmethodstub returnfalse; } @Override publicbooleanonDoubleTapEvent(MotionEvente){ //TODOAuto-generatedmethodstub returnfalse; } @Override publicvoidonClick(Viewv){ if(v==next){ flipper.showNext(); }else{ flipper.showPrevious(); } } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。