首页 > 系统 > Android > 正文

Android编程之界面跳动提示动画效果实现方法

2019-10-24 20:33:47
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android编程之界面跳动提示动画效果实现方法,实例分析了Android动画效果的布局及功能相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android编程之界面跳动提示动画效果实现方法。分享给大家供大家参考,具体如下:

上一个效果图:

Android编程之界面跳动提示动画效果实现方法

Android编程之界面跳动提示动画效果实现方法

先上布局:

 

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2. xmlns:tools="http://schemas.android.com/tools" 
  3. android:layout_width="match_parent" 
  4. android:layout_height="match_parent" > 
  5. <RelativeLayout 
  6. android:id="@+id/red" 
  7. android:layout_width="match_parent" 
  8. android:layout_height="match_parent" 
  9. android:background="#ff0000" > 
  10. <TextView 
  11. android:layout_width="wrap_content" 
  12. android:layout_height="wrap_content" 
  13. android:layout_above="@+id/switch2blue" 
  14. android:layout_centerHorizontal="true" 
  15. android:text="首页" /> 
  16. <Button 
  17. android:id="@+id/switch2blue" 
  18. android:layout_width="wrap_content" 
  19. android:layout_height="wrap_content" 
  20. android:layout_centerInParent="true" 
  21. android:text="置换位蓝色" /> 
  22. </RelativeLayout> 
  23. <RelativeLayout 
  24. android:id="@+id/blue" 
  25. android:layout_width="match_parent" 
  26. android:layout_height="match_parent" 
  27. android:background="#0000ff" > 
  28. <TextView 
  29. android:layout_width="wrap_content" 
  30. android:layout_height="wrap_content" 
  31. android:layout_above="@+id/switch2red" 
  32. android:layout_centerHorizontal="true" 
  33. android:text="第二页" /> 
  34. <Button 
  35. android:id="@+id/switch2red" 
  36. android:layout_width="wrap_content" 
  37. android:layout_height="wrap_content" 
  38. android:layout_alignParentBottom="true" 
  39. android:layout_centerHorizontal="true" 
  40. android:text="置换位红色" /> 
  41. </RelativeLayout> 
  42. </RelativeLayout> 

代码如下:

 

 
  1. import java.lang.reflect.Field; 
  2. import android.app.Activity; 
  3. import android.os.Bundle; 
  4. import android.os.Handler; 
  5. import android.util.DisplayMetrics; 
  6. import android.view.GestureDetector; 
  7. import android.view.MotionEvent; 
  8. import android.view.View; 
  9. import android.view.GestureDetector.OnGestureListener; 
  10. import android.view.View.OnClickListener; 
  11. import android.view.View.OnTouchListener; 
  12. import android.view.animation.AccelerateDecelerateInterpolator; 
  13. import android.view.animation.AccelerateInterpolator; 
  14. import android.view.animation.Animation; 
  15. import android.view.animation.AnimationSet; 
  16. import android.view.animation.TranslateAnimation; 
  17. import android.view.animation.Animation.AnimationListener; 
  18. import android.widget.Button; 
  19. import android.widget.RelativeLayout; 
  20. public class MainActivity extends Activity implements OnClickListener, 
  21. OnTouchListener, OnGestureListener { 
  22. private RelativeLayout red, blue; 
  23. private Button switch2blue, switch2red; 
  24. private float thisDelta = 0.05f; 
  25. private static boolean hasEverPulled = false
  26. private boolean pulled = false
  27. private static int height; 
  28. private GestureDetector gestureDetector; 
  29. private static int statusHeight = 0; 
  30. @Override 
  31. protected void onCreate(Bundle savedInstanceState) { 
  32. super.onCreate(savedInstanceState); 
  33. setContentView(R.layout.activity_main); 
  34. DisplayMetrics metrics = getResources().getDisplayMetrics(); 
  35. height = metrics.heightPixels; 
  36. statusHeight = getStatusHeight(); 
  37. initView(); 
  38. // 跳动提示可以上拉 
  39. final Handler handler = new Handler(); 
  40. handler.postDelayed(new Runnable() { 
  41. @Override 
  42. public void run() { 
  43. jump(thisDelta); 
  44. // handler.postDelayed(this, 3000); 
  45. }, 3000); 
  46. private void initView() { 
  47. red = (RelativeLayout) findViewById(R.id.red); 
  48. blue = (RelativeLayout) findViewById(R.id.blue); 
  49. switch2blue = (Button) findViewById(R.id.switch2blue); 
  50. switch2red = (Button) findViewById(R.id.switch2red); 
  51. switch2blue.setOnClickListener(this); 
  52. switch2red.setOnClickListener(this); 
  53. blue.setOnTouchListener(this); 
  54. blue.setLongClickable(true); 
  55. gestureDetector = new GestureDetector(thisthis); 
  56. @Override 
  57. public void onClick(View arg0) { 
  58. // TODO Auto-generated method stub 
  59. switch (arg0.getId()) { 
  60. case R.id.switch2blue: 
  61. red2BlueUI(); 
  62. break
  63. case R.id.switch2red: 
  64. blue2RedUI(); 
  65. break
  66. // 从红页面转到蓝页面 
  67. private void red2BlueUI() { 
  68. blue.bringToFront(); 
  69. blue.requestLayout(); 
  70. blue.invalidate(); 
  71. // 从蓝页面转到红页面 
  72. private void blue2RedUI() { 
  73. red.bringToFront(); 
  74. red.requestLayout(); 
  75. red.invalidate(); 
  76. // 获取状态栏的高度 
  77. private int getStatusHeight() { 
  78. Class<?> c = null
  79. Object obj = null
  80. Field field = null
  81. int x = 0; 
  82. int height = 0; 
  83. try { 
  84. c = Class.forName("com.android.internal.R$dimen"); 
  85. obj = c.newInstance(); 
  86. field = c.getField("status_bar_height"); 
  87. x = Integer.parseInt(field.get(obj).toString()); 
  88. height = getResources().getDimensionPixelSize(x); 
  89. catch (Exception e) { 
  90. e.printStackTrace(); 
  91. return height; 
  92. // 主页面跳动 
  93. private void jump(float delta) { 
  94. if (thisDelta - 0.03f < 0.001f) { 
  95. thisDelta = 0.05f; 
  96. return
  97. thisDelta = delta; 
  98. if (hasEverPulled) { 
  99. return
  100. playJumpAnimation(thisDelta); 
  101. // 上拉/下拉主页面 
  102. private void pull(boolean upward) { 
  103. if (upward && pulled) { 
  104. return
  105. if (!upward && !pulled) { 
  106. return
  107. float originalY; 
  108. float finalY; 
  109. if (!pulled) { 
  110. originalY = 0; 
  111. finalY = (float) (0 - height + 0.4 * height); 
  112. else { 
  113. originalY = (float) (0 - height + 0.4 * height); 
  114. finalY = 0; 
  115. pulled = !pulled; 
  116. AnimationSet animationSet = new AnimationSet(true); 
  117. animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, 
  118. finalY)); 
  119. animationSet.setDuration(300); 
  120. animationSet.setInterpolator(new AccelerateDecelerateInterpolator()); 
  121. animationSet.setFillAfter(true); 
  122. animationSet.setAnimationListener(new AnimationListener() { 
  123. @Override 
  124. public void onAnimationStart(Animation animation) { 
  125. if (!pulled) { 
  126. red2BlueUI(); 
  127. @Override 
  128. public void onAnimationRepeat(Animation animation) { 
  129. @Override 
  130. public void onAnimationEnd(Animation animation) { 
  131. // if (pulled) { 
  132. // blue2RedUI(); 
  133. // } 
  134. }); 
  135. blue.startAnimation(animationSet); 
  136. hasEverPulled = true
  137. // 跳起动画 
  138. private void playJumpAnimation(final float delta) { 
  139. float originalY = 0; 
  140. float finalY = 0 - height * delta; 
  141. AnimationSet animationSet = new AnimationSet(true); 
  142. animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, 
  143. finalY)); 
  144. animationSet.setDuration(300); 
  145. animationSet.setInterpolator(new AccelerateDecelerateInterpolator()); 
  146. animationSet.setFillAfter(true); 
  147. animationSet.setAnimationListener(new AnimationListener() { 
  148. @Override 
  149. public void onAnimationStart(Animation animation) { 
  150. @Override 
  151. public void onAnimationRepeat(Animation animation) { 
  152. @Override 
  153. public void onAnimationEnd(Animation animation) { 
  154. playLandAnimation(delta); 
  155. }); 
  156. blue.startAnimation(animationSet); 
  157. // 落下动画 
  158. private void playLandAnimation(final float delta) { 
  159. float originalY = 0 - height * delta; 
  160. float finalY = 0; 
  161. AnimationSet animationSet = new AnimationSet(true); 
  162. animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, 
  163. finalY)); 
  164. animationSet.setDuration(200); 
  165. animationSet.setInterpolator(new AccelerateInterpolator()); 
  166. animationSet.setFillAfter(true); 
  167. animationSet.setAnimationListener(new AnimationListener() { 
  168. @Override 
  169. public void onAnimationStart(Animation animation) { 
  170. @Override 
  171. public void onAnimationRepeat(Animation animation) { 
  172. @Override 
  173. public void onAnimationEnd(Animation animation) { 
  174. jump(0.03f); 
  175. }); 
  176. blue.startAnimation(animationSet); 
  177. @Override 
  178. public boolean onDown(MotionEvent e) { 
  179. pull(false); 
  180. return false
  181. @Override 
  182. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
  183. float velocityY) { 
  184. // 手势滑动达到100才触发 
  185. if (e1.getY() - e2.getY() > 100) { 
  186. pull(true); 
  187. else if (e2.getY() >= e1.getY()) { 
  188. pull(false); 
  189. return false
  190. @Override 
  191. public void onLongPress(MotionEvent e) { 
  192. @Override 
  193. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
  194. float distanceY) { 
  195. return false
  196. @Override 
  197. public void onShowPress(MotionEvent e) { 
  198. @Override 
  199. public boolean onSingleTapUp(MotionEvent e) { 
  200. return false
  201. @Override 
  202. public boolean onTouch(View v, MotionEvent event) { 
  203. if (pulled) { 
  204. // 首张页可触控点 
  205. if (event.getY() > height * 0.4 - statusHeight) { 
  206. return false
  207. return gestureDetector.onTouchEvent(event); 

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表