1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android ViewFlipper滑动屏幕切换

Android ViewFlipper滑动屏幕切换

时间:2023-04-22 07:56:57

相关推荐

Android ViewFlipper滑动屏幕切换

最近看到ViewFlipper和Animation在一起的用法,感觉很好,我就自己写了一下,感觉灰常好用,效果比gallery的滚动查看图片好用多了,这个也是实现滚动屏幕切换图片,但是用户体验要好很多!所以我把自己写的demo分享一下!希望对大家有用,也方便我自己以后查阅!转载请标明出处:

/wdaming1986/article/details/6766058

程序一开始界面,第一张图片: 向右滑动屏幕切换到第二张图片:

向右滑动切换第三张图片:

向左滑动就又切换到第二张图片了!

以此类推!

代码奉上:

一、MainActivity。java类文件:

[java]view plaincopyprint?.daming; importandroid.app.Activity; importandroid.content.Intent; importandroid.graphics.Color; importandroid.graphics.drawable.GradientDrawable; importandroid.graphics.drawable.GradientDrawable.Orientation; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.GestureDetector; importandroid.view.GestureDetector.OnGestureListener; importandroid.view.LayoutInflater; importandroid.view.MotionEvent; importandroid.view.View; importandroid.view.animation.AnimationUtils; importandroid.widget.ViewFlipper; publicclassMainActivityextendsActivityimplementsOnGestureListener{ privateGestureDetectordetector; privateViewFlipperflipper; privatefinalintHELPFILP_RESULT=106; IntentgetMainActivity=null; intcount=1; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); drawBackground(); LayoutInflaterinflater=LayoutInflater.from(this); finalViewlayout=inflater.inflate(R.layout.view_flipper,null); setContentView(layout); flipper=(ViewFlipper)findViewById(R.id.view_flipper); detector=newGestureDetector(this); } publicvoiddrawBackground() { GradientDrawablegrad=newGradientDrawable( Orientation.TL_BR, newint[]{Color.rgb(0,0,127), Color.rgb(0,0,255), Color.rgb(127,0,255), Color.rgb(127,127,255), Color.rgb(127,255,255), Color.rgb(255,255,255)} ); this.getWindow().setBackgroundDrawable(grad); } @Override publicbooleanonTouchEvent(MotionEventevent){ returnthis.detector.onTouchEvent(event); } publicbooleanonDown(MotionEventarg0){ //TODOAuto-generatedmethodstub returnfalse; } publicbooleanonFling(MotionEvente1,MotionEvente2,floatarg2, floatarg3){ Log.i("Fling","FlingHappened!"); if(e1.getX()-e2.getX()>5){ this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); if(count<3){ this.flipper.showNext(); count++; } returntrue; }elseif(e1.getX()-e2.getX()<-5){ this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in)); this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out)); if(count>1){ this.flipper.showPrevious(); count--; } returntrue; } returntrue; } publicvoidonLongPress(MotionEventarg0){ //TODOAuto-generatedmethodstub } publicbooleanonScroll(MotionEventarg0,MotionEventarg1,floatarg2, floatarg3){ //TODOAuto-generatedmethodstub returnfalse; } publicvoidonShowPress(MotionEventarg0){ //TODOAuto-generatedmethodstub } publicbooleanonSingleTapUp(MotionEventarg0){ //TODOAuto-generatedmethodstub returnfalse; } }

二、view_flipper.xml布局文件

[html]view plaincopyprint?<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="/apk/res/android" android:id="@+id/layout"android:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent" > <ViewFlipperandroid:id="@+id/view_flipper" 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"> <LinearLayoutandroid:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent"> <ImageViewandroid:id="@+id/view_bg1"android:src="@drawable/bg1" android:layout_width="fill_parent"android:layout_height="fill_parent" > </ImageView> </LinearLayout> <LinearLayoutandroid:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent"> <ImageViewandroid:id="@+id/view_bg2"android:src="@drawable/bg2" android:layout_width="fill_parent"android:layout_height="fill_parent" > </ImageView> </LinearLayout> <LinearLayoutandroid:orientation="horizontal" android:layout_width="fill_parent"android:layout_height="fill_parent"> <ImageViewandroid:id="@+id/view_bg3"android:src="@drawable/bg3" android:layout_width="fill_parent"android:layout_height="fill_parent" > </ImageView> </LinearLayout> </ViewFlipper> </LinearLayout>

三、anim动画布局文件:

1、push_left_in.xml布局文件:

[html]view plaincopyprint?<spanstyle="font-size:13px;"><?xmlversion="1.0"encoding="utf-8"?> <setxmlns:android="/apk/res/android"> <translateandroid:fromXDelta="100%p"android:toXDelta="0" android:duration="500"/> <alphaandroid:fromAlpha="0.1"android:toAlpha="1.0" android:duration="500"/> </set></span>[html]view plaincopyprint?<strong> </strong>

2、push_left_out.xml布局文件

[html]view plaincopyprint?<?xmlversion="1.0"encoding="utf-8"?> <setxmlns:android="/apk/res/android"> <translateandroid:fromXDelta="0"android:toXDelta="-100%p" android:duration="500"/> <alphaandroid:fromAlpha="1.0"android:toAlpha="0.1" android:duration="500"/> </set>

3、push_right_in.xml布局文件

[html]view plaincopyprint?<spanstyle="font-size:13px;color:#000000;"><?xmlversion="1.0"encoding="UTF-8"?> <setxmlns:android="/apk/res/android"> <translateandroid:fromXDelta="-100%p"android:toXDelta="0" android:duration="500"/> <alphaandroid:fromAlpha="0.1"android:toAlpha="1.0" android:duration="500"/> </set></span>

4、push_right_out.xml布局文件

[html]view plaincopyprint?<?xmlversion="1.0"encoding="UTF-8"?> <setxmlns:android="/apk/res/android"> <translateandroid:fromXDelta="0"android:toXDelta="100%p" android:duration="500"/> <alphaandroid:fromAlpha="1.0"android:toAlpha="0.1" android:duration="500"/> </set>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。