当前位置: 代码迷 >> Android >> [转][Android]滑动银屏效果GestureDetector、OnGestureListener、ViewFlipper
  详细解决方案

[转][Android]滑动银屏效果GestureDetector、OnGestureListener、ViewFlipper

热度:254   发布时间:2016-05-01 17:25:22.0
[转][Android]滑动屏幕效果GestureDetector、OnGestureListener、ViewFlipper

原文:http://blog.csdn.net/feng88724/article/details/6170694

?

?

屏幕切换指的是在同一个Activity内屏幕见的切换。

?

通过GestureDetector、OnGestureListener实现滑屏事件。ViewFlipper是继承至FrameLayout的,所以它是一个Layout里面可以放置多个View。示例中第一页仅放了一个按钮BUTTON,向下滑屏时,每页都只放了一个TEXTVIEW。

?

页面:

?

view plainprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:orientation="vertical"??
  4. ????android:layout_width="fill_parent"??
  5. ????android:layout_height="fill_parent"??
  6. ????>??
  7. ????<ViewFlipper?android:id="@+id/ViewFlipper01"??
  8. ????????android:layout_width="fill_parent"?android:layout_height="fill_parent">??
  9. ????</ViewFlipper>??
  10. </LinearLayout>??
?

?

?

Activity代码:

?

view plainprint?
  1. package?com.yfz;??
  2. import?android.app.Activity;??
  3. import?android.os.Bundle;??
  4. import?android.util.Log;??
  5. import?android.view.GestureDetector;??
  6. import?android.view.MotionEvent;??
  7. import?android.view.View;??
  8. import?android.view.GestureDetector.OnGestureListener;??
  9. import?android.view.ViewGroup.LayoutParams;??
  10. import?android.view.animation.AnimationUtils;??
  11. import?android.widget.Button;??
  12. import?android.widget.ExpandableListView;??
  13. import?android.widget.TextView;??
  14. import?android.widget.ViewFlipper;??
  15. public?class?Flip?extends?Activity?implements?OnGestureListener?{??
  16. ??????
  17. ????private?GestureDetector?detector;??
  18. ????private?ViewFlipper?flipper;??
  19. ??????
  20. ????/**?Called?when?the?activity?is?first?created.?*/??
  21. ????@Override??
  22. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  23. ????????super.onCreate(savedInstanceState);??
  24. ????????setContentView(R.layout.main);??
  25. ??????????
  26. ????????flipper?=?(ViewFlipper)?this.findViewById(R.id.ViewFlipper01);??
  27. ??????????
  28. ????????flipper.addView(addButtonByText("按钮"),new?LayoutParams(LayoutParams.FILL_PARENT,?LayoutParams.WRAP_CONTENT));??
  29. ??????????
  30. ????????detector?=?new?GestureDetector(this);??
  31. ????}??
  32. ??????
  33. ????public?View?addButtonByText(String?text){??
  34. ????????Button?btn?=?new?Button(this);????
  35. ????????btn.setText(text);????
  36. ????????return?btn;????
  37. ????}????
  38. ????public?View?addTextByText(String?text){??
  39. ????????TextView?tv?=?new?TextView(this);????
  40. ????????tv.setText(text);????
  41. ????????tv.setGravity(1);????
  42. ????????return?tv;????
  43. ????}????
  44. ??????
  45. ????@Override??
  46. ????public?boolean?onTouchEvent(MotionEvent?event)?{??
  47. ????????Log.i("Fling",?"Activity?onTouchEvent!");??
  48. ????????return?this.detector.onTouchEvent(event);??
  49. ????}??
  50. ????@Override??
  51. ????public?boolean?onDown(MotionEvent?e)?{??
  52. ????????//?TODO?Auto-generated?method?stub??
  53. ????????return?false;??
  54. ????}??
  55. ??????
  56. ????/**?
  57. ?????*?监听滑动?
  58. ?????*/??
  59. ????@Override??
  60. ????public?boolean?onFling(MotionEvent?e1,?MotionEvent?e2,?float?velocityX,??
  61. ????????????float?velocityY)?{??
  62. ????????//?TODO?Auto-generated?method?stub??
  63. ????????Log.i("Fling",?"Fling?Happened!");??
  64. ????????if?(e1.getX()?-?e2.getX()?>?120)?{??
  65. ????????????this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));??
  66. ????????????this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));??
  67. ????????????this.flipper.addView(addTextByText("文本框"),new?LayoutParams(LayoutParams.FILL_PARENT,?LayoutParams.WRAP_CONTENT));??
  68. ????????????this.flipper.showNext();??
  69. ????????????return?true;??
  70. ????????}?else?if?(e1.getX()?-?e2.getX()?<?-120)?{??
  71. ????????????this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_in));??
  72. ????????????this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_out));??
  73. ????????????this.flipper.showPrevious();??
  74. ????????????return?true;??
  75. ????????}??
  76. ????????return?true;??
  77. ????}??
  78. ????@Override??
  79. ????public?void?onLongPress(MotionEvent?e)?{??
  80. ????????//?TODO?Auto-generated?method?stub??
  81. ??????????
  82. ????}??
  83. ????@Override??
  84. ????public?boolean?onScroll(MotionEvent?e1,?MotionEvent?e2,?float?distanceX,??
  85. ????????????float?distanceY)?{??
  86. ????????//?TODO?Auto-generated?method?stub??
  87. ????????return?false;??
  88. ????}??
  89. ????@Override??
  90. ????public?void?onShowPress(MotionEvent?e)?{??
  91. ????????//?TODO?Auto-generated?method?stub??
  92. ??????????
  93. ????}??
  94. ????@Override??
  95. ????public?boolean?onSingleTapUp(MotionEvent?e)?{??
  96. ????????//?TODO?Auto-generated?method?stub??
  97. ????????return?false;??
  98. ????}??
  99. }??
?

?

?

这里实现的功能是从右往左滑动则切换到上一个View,从左往右滑动则切换到下一个View,并且使用不同的in、out 动画使切换效果看起来统一一些。

?

滑屏实现:

?

通过android.view.GestureDetector类可以检测各种手势事件,该类有两个回调接口分别用来通知具体的事件:
?
GestureDetector.OnDoubleTapListener:用来通知DoubleTap事件,类似于鼠标的双击事件,该接口有如下三个回调函数:
?
1.???onDoubleTap(MotionEvent e):通知DoubleTap手势,
2.???onDoubleTapEvent(MotionEvent e):通知DoubleTap手势中的事件,包含down、up和move事件(这里指的是在双击之间发生的事件,例如在同一个地方双击会产生DoubleTap手势,而在DoubleTap手势里面还会发生down和up事件,这两个事件由该函数通知);
3.???onSingleTapConfirmed(MotionEvent e):用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。
GestureDetector.OnGestureListener:用来通知普通的手势事件,该接口有如下六个回调函数:
1.???onDown(MotionEvent e):down事件;
2.???onSingleTapUp(MotionEvent e):一次点击up事件;
3.???onShowPress(MotionEvent e):down事件发生而move或则up还没发生前触发该事件;
4.???onLongPress(MotionEvent e):长按事件;
5.???onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滑动手势事件;
6.???onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):在屏幕上拖动事件。
本次仅用到了OnGestureListener类中的onFling方法。 ?其他手势事件各位可以自己回去试验。
简单说明:
要实现屏幕切换的话,首先需要定义一个GestureDetector:
private?GestureDetector mGestureDetector;
?
并在onCreate函数中初始化:
mGestureDetector =?new?GestureDetector(this);
?
同时Activity要继承OnGestureListener接口,并实现其中的onFling方法。
另外Activity的onTouchEvent事件也要实现!!
另外本例View切换时还有动画效果。使用Animation类实现,相关的函数:?
  • setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本:
一个接受单个参数,类型为android.view.animation.Animation;
一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。
  • setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。
  • showNext: 调用该函数来显示FrameLayout里面的下一个View。
  • showPrevious: 调用该函数来显示FrameLayout里面的上一个View。

?

动画源文件: (在res目录下,创建一个anim文件夹,把下面的文件都放在这里)

push_left_in.xml

?

view plainprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <set?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ????<translate?android:fromXDelta="100%p"?android:toXDelta="0"??
  4. ????????android:duration="500"?/>??
  5. ????<alpha?android:fromAlpha="0.1"?android:toAlpha="1.0"??
  6. ????????android:duration="500"?/>??
  7. </set>??
?

?

?

push_left_out.xml:

?

view plainprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <set?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ????<translate?android:fromXDelta="0"?android:toXDelta="-100%p"??
  4. ????????android:duration="500"?/>??
  5. ????<alpha?android:fromAlpha="1.0"?android:toAlpha="0.1"??
  6. ????????android:duration="500"?/>??
  7. </set>??
?

?

?

push_right_in.xml:

?

view plainprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <set?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ????<translate?android:fromXDelta="-100%p"?android:toXDelta="0"??
  4. ????????android:duration="500"?/>??
  5. ????<alpha?android:fromAlpha="0.1"?android:toAlpha="1.0"??
  6. ????????android:duration="500"?/>??
  7. </set>??
?

?

?

push_right_out.xml:

?

view plainprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <set?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ????<translate?android:fromXDelta="0"?android:toXDelta="100%p"??
  4. ????????android:duration="500"?/>??
  5. ????<alpha?android:fromAlpha="1.0"?android:toAlpha="0.1"??
  6. ????????android:duration="500"?/>??
  7. </set>??
?

?

?

  相关解决方案