Android手势识别ViewFlipper触摸动画

2010-11-01 09:46 ideasandroid ideasandroid 我要评论(0) 字号: T | T 一键收藏,随时查看,分享好友! 今天给大家介绍一下如何实现Android主页面的左右拖动效果。使用ViewFlipper来将您要来回拖动的View装在一起,然后与GestureDetector手势识别类来联动,确定要显示哪个View,加上一点点动画效果即可。 标签: View Android AD: 限时报名参加“甲骨文全球大会・2010・北京”及“JavaOne和甲骨文开发者大会2010”

  • 索 引
  • [显示]
我们曾介绍过“ 在Android开发中使用Gallery实现'多级联动'”和“ 在Android中实现service动态更新UI界面”。今天给大家介绍一下如何实现Android主页面的左右拖动效果。实现起来很简单,就是使用ViewFlipper来将您要来回拖动的View装在一起,然后与GestureDetector手势识别类来联动,确定要显示哪个View,加上一点点动画效果即可。比如当手指向左快速滑动时跳转到上一个View,手指向右快速滑动时跳转到下一个View,本例中使用图片作为各个View的页面,实现左右快速滑动显示不同的图片。 Android
Android View 首先来看看我们的layout,如下所示:
                    
  1. <linearlayoutandroidandroid:layout_height="fill_parent"android:layout_width="fill_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android">
  2. <viewflipperandroidandroid:id="@+id/flipper"android:layout_below="@+id/CockpitLayout"android:layout_height="fill_parent"android:layout_width="fill_parent">
  3. <includeandroid:id="@+id/firstlayout"layout="@layout/first">
  4. <includeandroid:id="@+id/secondlayout"layout="@layout/second">
  5. <includeandroid:id="@+id/thirdlayout"layout="@layout/third">
  6. <includeandroid:id="@+id/fourthlayout"layout="@layout/fourth">
  7. </include></include></include></include></viewflipper>
  8. </linearlayout>
如上所示,在ViewFlipper中放置多个layout(接下来会在不同的layout中来回滑动),ViewFlipper在同一个页面就显示其中一个layout。 ViewFlipper中的四个layout很简单,我们就放置一张图片,如下所示:
                    
  1. <linearlayoutandroidandroid:gravity="center_vertical"android:layout_height="fill_parent"android:layout_width="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
  2. <imageviewandroidandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@drawable/v1">
  3. </imageview></linearlayout>
接下来我们来看看Activity,我们的Activity需要实现两个接口OnGestureListener,OnTouchListener。具体的代码如下所示,代码中都有相应的注释,这里就不再详述。
                    
  1. packagecom.ideasandroid.demo;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.GestureDetector;
  5. importandroid.view.MotionEvent;
  6. importandroid.view.View;
  7. importandroid.view.GestureDetector.OnGestureListener;
  8. importandroid.view.View.OnTouchListener;
  9. importandroid.view.animation.AccelerateInterpolator;
  10. importandroid.view.animation.Animation;
  11. importandroid.view.animation.TranslateAnimation;
  12. importandroid.widget.ViewFlipper;
  13. publicclassViewFlipperDemoextendsActivityimplementsOnGestureListener,OnTouchListener{
  14. privateViewFlippermFlipper;
  15. GestureDetectormGestureDetector;
  16. privateintmCurrentLayoutState;
  17. privatestaticfinalintFLING_MIN_DISTANCE=100;
  18. privatestaticfinalintFLING_MIN_VELOCITY=200;
  19. @Override
  20. publicvoidonCreate(BundlesavedInstanceState){
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. mFlipper=(ViewFlipper)findViewById(R.id.flipper);
  24. //注册一个用于手势识别的类
  25. mGestureDetector=newGestureDetector(this);
  26. //给mFlipper设置一个listener
  27. mFlipper.setOnTouchListener(this);
  28. mCurrentLayoutState=0;
  29. //允许长按住ViewFlipper,这样才能识别拖动等手势
  30. mFlipper.setLongClickable(true);
  31. }
  32. /**
  33. *此方法在本例中未用到,可以指定跳转到某个页面
  34. *@paramswitchTo
  35. */
  36. publicvoidswitchLayoutStateTo(intswitchTo){
  37. while(mCurrentLayoutState!=switchTo){
  38. if(mCurrentLayoutState>switchTo){
  39. mCurrentLayoutState--;
  40. mFlipper.setInAnimation(inFromLeftAnimation());
  41. mFlipper.setOutAnimation(outToRightAnimation());
  42. mFlipper.showPrevious();
  43. }else{
  44. mCurrentLayoutState++;
  45. mFlipper.setInAnimation(inFromRightAnimation());
  46. mFlipper.setOutAnimation(outToLeftAnimation());
  47. mFlipper.showNext();
  48. }
  49. }
  50. ;
  51. }
  52. /**
  53. *定义从右侧进入的动画效果
  54. *@return
  55. */
  56. protectedAnimationinFromRightAnimation(){
  57. AnimationinFromRight=newTranslateAnimation(
  58. Animation.RELATIVE_TO_PARENT,+1.0f,
  59. Animation.RELATIVE_TO_PARENT,0.0f,
  60. Animation.RELATIVE_TO_PARENT,0.0f,
  61. Animation.RELATIVE_TO_PARENT,0.0f);
  62. inFromRight.setDuration(500);
  63. inFromRight.setInterpolator(newAccelerateInterpolator());
  64. returninFromRight;
  65. }
  66. /**
  67. *定义从左侧退出的动画效果
  68. *@return
  69. */
  70. protectedAnimationoutToLeftAnimation(){
  71. AnimationouttoLeft=newTranslateAnimation(
  72. Animation.RELATIVE_TO_PARENT,0.0f,
  73. Animation.RELATIVE_TO_PARENT,-1.0f,
  74. Animation.RELATIVE_TO_PARENT,0.0f,
  75. Animation.RELATIVE_TO_PARENT,0.0f);
  76. outtoLeft.setDuration(500);
  77. outtoLeft.setInterpolator(newAccelerateInterpolator());
  78. returnouttoLeft;
  79. }
  80. /**
  81. *定义从左侧进入的动画效果
  82. *@return
  83. */
  84. protectedAnimationinFromLeftAnimation(){
  85. AnimationinFromLeft=newTranslateAnimation(
  86. Animation.RELATIVE_TO_PARENT,-1.0f,
  87. Animation.RELATIVE_TO_PARENT,0.0f,
  88. Animation.RELATIVE_TO_PARENT,0.0f,
  89. Animation.RELATIVE_TO_PARENT,0.0f);
  90. inFromLeft.setDuration(500);
  91. inFromLeft.setInterpolator(newAccelerateInterpolator());
  92. returninFromLeft;
  93. }
  94. /**
  95. *定义从右侧退出时的动画效果
  96. *@return
  97. */
  98. protectedAnimationoutToRightAnimation(){
  99. AnimationouttoRight=newTranslateAnimation(
  100. Animation.RELATIVE_TO_PARENT,0.0f,
  101. Animation.RELATIVE_TO_PARENT,+1.0f,
  102. Animation.RELATIVE_TO_PARENT,0.0f,
  103. Animation.RELATIVE_TO_PARENT,0.0f);
  104. outtoRight.setDuration(500);
  105. outtoRight.setInterpolator(newAccelerateInterpolator());
  106. returnouttoRight;
  107. }
  108. publicbooleanonDown(MotionEvente){
  109. //TODOAuto-generatedmethodstub
  110. returnfalse;
  111. }
  112. /*
  113. *用户按下触摸屏、快速移动后松开即触发这个事件
  114. *e1:第1个ACTION_DOWNMotionEvent
  115. *e2:最后一个ACTION_MOVEMotionEvent
  116. *velocityX:X轴上的移动速度,像素/秒
  117. *velocityY:Y轴上的移动速度,像素/秒
  118. *触发条件:
  119. *X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
  120. */
  121. publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,
  122. floatvelocityY){
  123. if(e1.getX()-e2.getX()>FLING_MIN_DISTANCE
  124. &&Math.abs(velocityX)>FLING_MIN_VELOCITY){
  125. //当像左侧滑动的时候
  126. //设置View进入屏幕时候使用的动画
  127. mFlipper.setInAnimation(inFromRightAnimation());
  128. //设置View退出屏幕时候使用的动画
  129. mFlipper.setOutAnimation(outToLeftAnimation());
  130. mFlipper.showNext();
  131. }elseif(e2.getX()-e1.getX()>FLING_MIN_DISTANCE
  132. &&Math.abs(velocityX)>FLING_MIN_VELOCITY){
  133. //当像右侧滑动的时候
  134. mFlipper.setInAnimation(inFromLeftAnimation());
  135. mFlipper.setOutAnimation(outToRightAnimation());
  136. mFlipper.showPrevious();
  137. }
  138. returnfalse;
  139. }
  140. publicvoidonLongPress(MotionEvente){
  141. //TODOAuto-generatedmethodstub
  142. }
  143. publicbooleanonScroll(MotionEvente1,MotionEvente2,floatdistanceX,
  144. floatdistanceY){
  145. returnfalse;
  146. }
  147. publicvoidonShowPress(MotionEvente){
  148. //TODOAuto-generatedmethodstub
  149. }
  150. publicbooleanonSingleTapUp(MotionEvente){
  151. //TODOAuto-generatedmethodstub
  152. returnfalse;
  153. }
  154. publicbooleanonTouch(Viewv,MotionEventevent){
  155. //一定要将触屏事件交给手势识别类去处理(自己处理会很麻烦的)
  156. returnmGestureDetector.onTouchEvent(event);
  157. }
  158. }
希望本文对您有所帮助! 【编辑推荐】
  1. 在Android开发中使用Gallery实现“多级联动”
  2. 在Android中实现service动态更新UI界面
  3. Android数据库事务浅析
  4. Android的UI设计与后台线程交互
  5. Android触屏textview及listview对比验证
【责任编辑: 立方 TEL:(010)68476606】
分享至 一键收藏,随时查看,分享好友!
0人 了这篇文章
类别:未分类┆阅读( 0)┆评论( 0) ┆ 返回博主首页┆ 返回博客首页 上一篇 利用SSIS打造MySQL监控工具 下一篇 项目管理学习笔记四:立项管理

更多相关文章

  1. Android中使用ViewPager制作广告栏滚屏效果
  2. Android之UI学习篇二:TextVeiw显示表情和跑马灯效果
  3. Android CountDownTimer带有动画的倒计时
  4. Android 自定义阴影效果详解及实例
  5. Android取消RecyclerView、ListView、ScrollView、HorizontalScr
  6. 图文详解Android属性动画
  7. Android Activity切换动画效果详解(附源代码)
  8. Android基础控件——ProgressBar自定义的介绍、动画效果实现、附
  9. Android 之手势识别篇-GestureDetector

随机推荐

  1. Android(安卓)ProgressBar 几乎全部的用
  2. 判断手机类型
  3. Android中ListVIew高度自适应,解决ScrollV
  4. Linearlayout 添加divider
  5. Android之Gridview
  6. Android(安卓)AsyncHttpClient
  7. android MediaPlayer出现RuntimeExceptio
  8. ubuntu下无法更新android sdk
  9. android读写文件
  10. android SpannableString使用详解,替代多