Android 手势 动画 ViewFlippe (1)

 
今天给大家介绍一下如何实现Android主页面的左右拖动效果。实现起来很简单,就是使用ViewFlipper来将您要来回拖动的View装在一起,然后与GestureDetector手势识别类来联动,确定要显示哪个View,加上一点点动画效果即可。比如当手指向左快速滑动时跳转到上一个View,手指向右快速滑动时跳转到下一个View,本例中使用图片作为各个View的页面,实现左右快速滑动显示不同的图片。


Android View



首先来看看我们的layout,如下所示:
 
<linearlayout androidandroid:layout_height="fill_parent"android:layout_width="fill_parent" android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android"> 


<viewflipper androidandroid:id="@+id/flipper"android:layout_below="@+id/CockpitLayout"android:layout_height="fill_parent" android:layout_width="fill_parent"> 


<include android:id="@+id/firstlayout" layout="@layout/first"> 


<include android:id="@+id/secondlayout" layout="@layout/second"> 


<include android:id="@+id/thirdlayout" layout="@layout/third"> 


<include android:id="@+id/fourthlayout" layout="@layout/fourth"> 


</include></include></include></include></viewflipper> 


</linearlayout> 



 
 
复制代码
如上所示,在ViewFlipper中放置多个layout(接下来会在不同的layout中来回滑动),ViewFlipper在同一个页面就显示其中一个layout。
ViewFlipper中的四个layout很简单,我们就放置一张图片,如下所示:
<linearlayout androidandroid:gravity="center_vertical"android:layout_height="fill_parent" android:layout_width="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"> 


<imageview androidandroid:layout_height="wrap_content"android:layout_width="wrap_content" android:src="@drawable/v1"> 


</imageview></linearlayout> 


 

复制代码

接下来我们来看看Activity,我们的Activity需要实现两个接口OnGestureListener,OnTouchListener。具体的代码如下所示,代码中都有相应的注释,这里就不再详述。
  1. package com.ideasandroid.demo; import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.GestureDetector;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.GestureDetector.OnGestureListener;
  7. import android.view.View.OnTouchListener;
  8. import android.view.animation.AccelerateInterpolator;
  9. import android.view.animation.Animation;
  10. import android.view.animation.TranslateAnimation;
  11. import android.widget.ViewFlipper;
  12. public class ViewFlipperDemo extends Activity implementsOnGestureListener,OnTouchListener{
  13. private ViewFlipper mFlipper;
  14. GestureDetector mGestureDetector;
  15. private int mCurrentLayoutState;
  16. private static final int FLING_MIN_DISTANCE = 100;
  17. private static final int FLING_MIN_VELOCITY = 200;

  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. mFlipper = (ViewFlipper) findViewById(R.id.flipper);
  23. //注册一个用于手势识别的类
  24. mGestureDetector = new GestureDetector(this);
  25. //给mFlipper设置一个listener
  26. mFlipper.setOnTouchListener(this);
  27. mCurrentLayoutState = 0;
  28. //允许长按住ViewFlipper,这样才能识别拖动等手势
  29. mFlipper.setLongClickable(true);
  30. }
  31. /**
  32. * 此方法在本例中未用到,可以指定跳转到某个页面
  33. * @param switchTo
  34. */
  35. public void switchLayoutStateTo(int switchTo) {
  36. while (mCurrentLayoutState != switchTo) {
  37. if (mCurrentLayoutState > switchTo) {
  38. mCurrentLayoutState--;
  39. mFlipper.setInAnimation(inFromLeftAnimation());
  40. mFlipper.setOutAnimation(outToRightAnimation());
  41. mFlipper.showPrevious();
  42. } else {
  43. mCurrentLayoutState++;
  44. mFlipper.setInAnimation(inFromRightAnimation());
  45. mFlipper.setOutAnimation(outToLeftAnimation());
  46. mFlipper.showNext();
  47. }

  48. }
  49. ;
  50. }

  51. /**
  52. * 定义从右侧进入的动画效果
  53. * @return
  54. */
  55. protected Animation inFromRightAnimation() {
  56. Animation inFromRight = new TranslateAnimation(
  57. Animation.RELATIVE_TO_PARENT, +1.0f,
  58. Animation.RELATIVE_TO_PARENT, 0.0f,
  59. Animation.RELATIVE_TO_PARENT, 0.0f,
  60. Animation.RELATIVE_TO_PARENT, 0.0f);
  61. inFromRight.setDuration(500);
  62. inFromRight.setInterpolator(new AccelerateInterpolator());
  63. return inFromRight;
  64. }
  65. /**
  66. * 定义从左侧退出的动画效果
  67. * @return */
  68. protected Animation outToLeftAnimation() {
  69. Animation outtoLeft = new TranslateAnimation(
  70. Animation.RELATIVE_TO_PARENT, 0.0f,
  71. Animation.RELATIVE_TO_PARENT, -1.0f,
  72. Animation.RELATIVE_TO_PARENT, 0.0f,
  73. Animation.RELATIVE_TO_PARENT, 0.0f);
  74. outtoLeft.setDuration(500);
  75. outtoLeft.setInterpolator(new AccelerateInterpolator());
  76. return outtoLeft;
  77. }

  78. /**
  79. * 定义从左侧进入的动画效果
  80. * @return
  81. */
  82. protected Animation inFromLeftAnimation() {
  83. Animation inFromLeft = new TranslateAnimation(
  84. Animation.RELATIVE_TO_PARENT, -1.0f,
  85. Animation.RELATIVE_TO_PARENT, 0.0f,
  86. Animation.RELATIVE_TO_PARENT, 0.0f,
  87. Animation.RELATIVE_TO_PARENT, 0.0f);
  88. inFromLeft.setDuration(500);
  89. inFromLeft.setInterpolator(new AccelerateInterpolator());
  90. return inFromLeft;
  91. }

  92. /**
  93. * 定义从右侧退出时的动画效果
  94. * @return
  95. */
  96. protected Animation outToRightAnimation() {
  97. Animation outtoRight = new TranslateAnimation(
  98. Animation.RELATIVE_TO_PARENT, 0.0f,
  99. Animation.RELATIVE_TO_PARENT, +1.0f,
  100. Animation.RELATIVE_TO_PARENT, 0.0f,
  101. Animation.RELATIVE_TO_PARENT, 0.0f);
  102. outtoRight.setDuration(500);
  103. outtoRight.setInterpolator(new AccelerateInterpolator());
  104. return outtoRight;
  105. }

  106. public boolean onDown(MotionEvent e) {
  107. // TODO Auto-generated method stub
  108. return false;
  109. }

  110. /*
  111. * 用户按下触摸屏、快速移动后松开即触发这个事件
  112. * e1:第1个ACTION_DOWN MotionEvent
  113. * e2:最后一个ACTION_MOVE MotionEvent
  114. * velocityX:X轴上的移动速度,像素/秒
  115. * velocityY:Y轴上的移动速度,像素/秒
  116. * 触发条件 :
  117. * X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
  118. */
  119. public boolean onFling(MotionEvent e1, MotionEvent e2, floatvelocityX,
  120. float velocityY) {
  121. if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
  122. && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
  123. // 当像左侧滑动的时候
  124. //设置View进入屏幕时候使用的动画
  125. mFlipper.setInAnimation(inFromRightAnimation());
  126. //设置View退出屏幕时候使用的动画
  127. mFlipper.setOutAnimation(outToLeftAnimation());
  128. mFlipper.showNext();
  129. } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
  130. && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
  131. // 当像右侧滑动的时候
  132. mFlipper.setInAnimation(inFromLeftAnimation());
  133. mFlipper.setOutAnimation(outToRightAnimation());
  134. mFlipper.showPrevious();
  135. }
  136. return false;
  137. }

  138. public void onLongPress(MotionEvent e) {
  139. // TODO Auto-generated method stub

  140. }
  141. public boolean onScroll(MotionEvent e1, MotionEvent e2, floatdistanceX,
  142. float distanceY) {
  143. return false;
  144. }

  145. public void onShowPress(MotionEvent e) {
  146. // TODO Auto-generated method stub

  147. }

  148. public boolean onSingleTapUp(MotionEvent e) {
  149. // TODO Auto-generated method stub
  150. return false;
  151. }
  152. public boolean onTouch(View v, MotionEvent event) {
  153. // 一定要将触屏事件交给手势识别类去处理(自己处理会很麻烦的)
  154. return mGestureDetector.onTouchEvent(event);
  155. }
  156. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值