Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android手勢識別ViewFlipper觸摸動畫

Android手勢識別ViewFlipper觸摸動畫

編輯:高級開發

我們曾介紹過“在android開發中使用Gallery實現'多級聯動'”和“在android中實現service動態更新UI界面”。今天給大家介紹一下如何實現android主頁面的左右拖動效果。實現起來很簡單,就是使用ViewFlipper來將您要來回拖動的View裝在一起,然後與GestureDetector手勢識別類來聯動,確定要顯示哪個View,加上一點點動畫效果即可。比如當手指向左快速滑動時跳轉到上一個View,手指向右快速滑動時跳轉到下一個View,本例中使用圖片作為各個VIEw的頁面,實現左右快速滑動顯示不同的圖片。

Android View
android VIEw

首先來看看我們的layout,如下所示:

  1. <linearlayout androidandroid:layout_height="fill_parent"android:layout_width="fill_parent" android:orIEntation="vertical"XMLns:android="http://schemas.android.com/apk/res/android">
  2. <vIEwflipper androidandroid:id="@+id/flipper"android:layout_below="@+id/CockpitLayout"android:layout_height="fill_parent" android:layout_width="fill_parent">
  3. <include android:id="@+id/firstlayout" layout="@layout/first">
  4. <include android:id="@+id/secondlayout" layout="@layout/second">
  5. <include android:id="@+id/thirdlayout" layout="@layout/third">
  6. <include android:id="@+id/fourthlayout" layout="@layout/fourth">
  7. </include></include></include></include></vIEwflipper>
  8. </linearlayout>

如上所示,在ViewFlipper中放置多個layout(接下來會在不同的layout中來回滑動),VIEwFlipper在同一個頁面就顯示其中一個layout。

VIEwFlipper中的四個layout很簡單,我們就放置一張圖片,如下所示:

  1. <linearlayout androidandroid:gravity="center_vertical"android:layout_height="fill_parent" android:layout_width="fill_parent"XMLns:android="http://schemas.android.com/apk/res/android">
  2. <imagevIEw androidandroid:layout_height="wrap_content"android:layout_width="wrap_content" android:src="@drawable/v1">
  3. </imagevIEw></linearlayout>

接下來我們來看看Activity,我們的Activity需要實現兩個接口OnGestureListener,OnTouchListener。具體的代碼如下所示,代碼中都有相應的注釋,這裡就不再詳述。

  1. package com.ideasandroid.demo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.vIEw.GestureDetector;
  5. import android.vIEw.MotionEvent;
  6. import android.view.VIEw;
  7. import android.vIEw.GestureDetector.OnGestureListener;
  8. import android.view.VIEw.OnTouchListener;
  9. import android.vIEw.animation.AccelerateInterpolator;
  10. import android.vIEw.animation.Animation;
  11. import android.vIEw.animation.TranslateAnimation;
  12. import android.widget.VIEwFlipper;
  13. public class VIEwFlipperDemo extends Activity implementsOnGestureListener,OnTouchListener{
  14. private VIEwFlipper mFlipper;
  15. GestureDetector mGestureDetector;
  16. private int mCurrentLayoutState;
  17. private static final int FLING_MIN_DISTANCE = 100;
  18. private static final int FLING_MIN_VELOCITY = 200;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentVIEw(R.layout.main);
  23. mFlipper = (ViewFlipper) findVIEwById(R.id.flipper);
  24. //注冊一個用於手勢識別的類
  25. mGestureDetector = new GestureDetector(this);
  26. //給mFlipper設置一個listener
  27. mFlipper.setOnTouchListener(this);
  28. mCurrentLayoutState = 0;
  29. //允許長按住VIEwFlipper,這樣才能識別拖動等手勢
  30. mFlipper.setLongClickable(true);
  31. }
  32. /**
  33. * 此方法在本例中未用到,可以指定跳轉到某個頁面
  34. * @param switchTo
  35. */
  36. public void switchLayoutStateTo(int switchTo) {
  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. protected Animation inFromRightAnimation() {
  57. Animation inFromRight = new TranslateAnimation(
  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(new AccelerateInterpolator());
  64. return inFromRight;
  65. }
  66. /**
  67. * 定義從左側退出的動畫效果
  68. * @return
  69. */
  70. protected Animation outToLeftAnimation() {
  71. Animation outtoLeft = new TranslateAnimation(
  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(new AccelerateInterpolator());
  78. return outtoLeft;
  79. }
  80. /**
  81. * 定義從左側進入的動畫效果
  82. * @return
  83. */
  84. protected Animation inFromLeftAnimation() {
  85. Animation inFromLeft = new TranslateAnimation(
  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(new AccelerateInterpolator());
  92. return inFromLeft;
  93. }
  94. /**
  95. * 定義從右側退出時的動畫效果
  96. * @return
  97. */
  98. protected Animation outToRightAnimation() {
  99. Animation outtoRight = new TranslateAnimation(
  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(new AccelerateInterpolator());
  106. return outtoRight;
  107. }
  108. public boolean onDown(MotionEvent e) {
  109. // TODO Auto-generated method stub
  110. return false;
  111. }
  112. /*
  113. * 用戶按下觸摸屏、快速移動後松開即觸發這個事件
  114. * e1:第1個ACTION_DOWN MotionEvent
  115. * e2:最後一個ACTION_MOVE MotionEvent
  116. * velocityX:X軸上的移動速度,像素/秒
  117. * velocityY:Y軸上的移動速度,像素/秒
  118. * 觸發條件 :
  119. * X軸的坐標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒
  120. */
  121. public boolean onFling(MotionEvent e1, MotionEvent e2, floatvelocityX,
  122. float velocityY) {
  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. } else if (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. return false;
  139. }
  140. public void onLongPress(MotionEvent e) {
  141. // TODO Auto-generated method stub
  142. }
  143. public boolean onScroll(MotionEvent e1, MotionEvent e2, floatdistanceX,
  144. float distanceY) {
  145. return false;
  146. }
  147. public void onShowPress(MotionEvent e) {
  148. // TODO Auto-generated method stub
  149. }
  150. public boolean onSingleTapUp(MotionEvent e) {
  151. // TODO Auto-generated method stub
  152. return false;
  153. }
  154. public boolean onTouch(VIEw v, MotionEvent event) {
  155. // 一定要將觸屏事件交給手勢識別類去處理(自己處理會很麻煩的)
  156. return mGestureDetector.onTouchEvent(event);
  157. }
  158. }

希望本文對您有所幫助!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved