歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android手勢識別ViewFlipper觸摸動畫

Android手勢識別ViewFlipper觸摸動畫

日期:2017/3/1 10:33:10   编辑:Linux編程
最近項目中用到了ViewFlipper這個類,感覺效果真的很炫,今天自己也試著做了下,確實還不錯。

首先在layout下定義viewflipper.xml

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <ViewFlipper
  7. android:id="@+id/viewFlipper1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent" >
  10. <ImageView
  11. android:id="@+id/imageView1"
  12. android:layout_width="fill_parent"
  13. android:layout_height="fill_parent"
  14. android:src="@drawable/b" />
  15. <ImageView
  16. android:id="@+id/imageView2"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:src="@drawable/c" />
  20. <ImageView
  21. android:id="@+id/imageView3"
  22. android:layout_width="fill_parent"
  23. android:layout_height="fill_parent"
  24. android:src="@drawable/d" />
  25. <ImageView
  26. android:id="@+id/imageView4"
  27. android:layout_width="fill_parent"
  28. android:layout_height="fill_parent"
  29. android:src="@drawable/f" />
  30. <ImageView
  31. android:id="@+id/imageView5"
  32. android:layout_width="fill_parent"
  33. android:layout_height="fill_parent"
  34. android:src="@drawable/g" />
  35. </ViewFlipper>
  36. </LinearLayout>
ViewFlipper中包含了5張圖片 用來手勢切換。

[html]

  1. public class ViewFlipperActivity extends Activity implements OnTouchListener, android.view.GestureDetector.OnGestureListener {
  2. private ViewFlipper flipper;
  3. GestureDetector mGestureDetector;
  4. private int mCurrentLayoutState;
  5. private static final int FLING_MIN_DISTANCE = 80;
  6. private static final int FLING_MIN_VELOCITY = 150;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. // TODO Auto-generated method stub
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.viewflipper);
  12. flipper=(ViewFlipper) this.findViewById(R.id.viewFlipper1);
  13. //注冊一個用於手勢識別的類
  14. mGestureDetector = new GestureDetector(this);
  15. //給mFlipper設置一個listener
  16. flipper.setOnTouchListener(this);
  17. mCurrentLayoutState = 0;
  18. //允許長按住ViewFlipper,這樣才能識別拖動等手勢
  19. flipper.setLongClickable(true);
  20. }
  21. /**
  22. * 此方法在本例中未用到,可以指定跳轉到某個頁面
  23. * @param switchTo
  24. */
  25. public void switchLayoutStateTo(int switchTo) {
  26. while (mCurrentLayoutState != switchTo) {
  27. if (mCurrentLayoutState > switchTo) {
  28. mCurrentLayoutState--;
  29. flipper.setInAnimation(inFromLeftAnimation());
  30. flipper.setOutAnimation(outToRightAnimation());
  31. flipper.showPrevious();
  32. } else {
  33. mCurrentLayoutState++;
  34. flipper.setInAnimation(inFromRightAnimation());
  35. flipper.setOutAnimation(outToLeftAnimation());
  36. flipper.showNext();
  37. }
  38. }
  39. }
  40. /**
  41. * 定義從右側進入的動畫效果
  42. * @return
  43. */
  44. protected Animation inFromRightAnimation() {
  45. Animation inFromRight = new TranslateAnimation(
  46. Animation.RELATIVE_TO_PARENT, +1.0f,
  47. Animation.RELATIVE_TO_PARENT, 0.0f,
  48. Animation.RELATIVE_TO_PARENT, 0.0f,
  49. Animation.RELATIVE_TO_PARENT, 0.0f);
  50. inFromRight.setDuration(200);
  51. inFromRight.setInterpolator(new AccelerateInterpolator());
  52. return inFromRight;
  53. }
  54. /**
  55. * 定義從左側退出的動畫效果
  56. * @return
  57. */
  58. protected Animation outToLeftAnimation() {
  59. Animation outtoLeft = new TranslateAnimation(
  60. Animation.RELATIVE_TO_PARENT, 0.0f,
  61. Animation.RELATIVE_TO_PARENT, -1.0f,
  62. Animation.RELATIVE_TO_PARENT, 0.0f,
  63. Animation.RELATIVE_TO_PARENT, 0.0f);
  64. outtoLeft.setDuration(200);
  65. outtoLeft.setInterpolator(new AccelerateInterpolator());
  66. return outtoLeft;
  67. }
  68. /**
  69. * 定義從左側進入的動畫效果
  70. * @return
  71. */
  72. protected Animation inFromLeftAnimation() {
  73. Animation inFromLeft = new TranslateAnimation(
  74. Animation.RELATIVE_TO_PARENT, -1.0f,
  75. Animation.RELATIVE_TO_PARENT, 0.0f,
  76. Animation.RELATIVE_TO_PARENT, 0.0f,
  77. Animation.RELATIVE_TO_PARENT, 0.0f);
  78. inFromLeft.setDuration(200);
  79. inFromLeft.setInterpolator(new AccelerateInterpolator());
  80. return inFromLeft;
  81. }
  82. /**
  83. * 定義從右側退出時的動畫效果
  84. * @return
  85. */
  86. protected Animation outToRightAnimation() {
  87. Animation outtoRight = new TranslateAnimation(
  88. Animation.RELATIVE_TO_PARENT, 0.0f,
  89. Animation.RELATIVE_TO_PARENT, +1.0f,
  90. Animation.RELATIVE_TO_PARENT, 0.0f,
  91. Animation.RELATIVE_TO_PARENT, 0.0f);
  92. outtoRight.setDuration(200);
  93. outtoRight.setInterpolator(new AccelerateInterpolator());
  94. return outtoRight;
  95. }
  96. public boolean onDown(MotionEvent e) {
  97. // TODO Auto-generated method stub
  98. return false;
  99. }
  100. /*
  101. * 用戶按下觸摸屏、快速移動後松開即觸發這個事件
  102. * e1:第1個ACTION_DOWN MotionEvent
  103. * e2:最後一個ACTION_MOVE MotionEvent
  104. * velocityX:X軸上的移動速度,像素/秒
  105. * velocityY:Y軸上的移動速度,像素/秒
  106. * 觸發條件 :
  107. * X軸的坐標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒
  108. */
  109. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  110. float velocityY) {
  111. if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
  112. && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
  113. // 當像左側滑動的時候
  114. //設置View進入屏幕時候使用的動畫
  115. flipper.setInAnimation(inFromRightAnimation());
  116. //設置View退出屏幕時候使用的動畫
  117. flipper.setOutAnimation(outToLeftAnimation());
  118. flipper.showNext();
  119. } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
  120. && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
  121. // 當像右側滑動的時候
  122. flipper.setInAnimation(inFromLeftAnimation());
  123. flipper.setOutAnimation(outToRightAnimation());
  124. flipper.showPrevious();
  125. }
  126. return false;
  127. }
  128. public void onLongPress(MotionEvent e) {
  129. // TODO Auto-generated method stub
  130. }
  131. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
  132. float distanceY) {
  133. // TODO Auto-generated method stub
  134. return false;
  135. }
  136. public void onShowPress(MotionEvent e) {
  137. // TODO Auto-generated method stub
  138. }
  139. public boolean onSingleTapUp(MotionEvent e) {
  140. // TODO Auto-generated method stub
  141. return false;
  142. }
  143. public boolean onTouch(View v, MotionEvent event) {
  144. // TODO Auto-generated method stub
  145. // 一定要將觸屏事件交給手勢識別類去處理(自己處理會很麻煩的)
  146. return mGestureDetector.onTouchEvent(event);
  147. }
  148. }
這樣就OK了 看下效果

因為是手勢識別效果不太好演示,所以大家自己實現了以後就可以看到效果啦!

Copyright © Linux教程網 All Rights Reserved