Andorid Activity跳转动画

下面的Demo是我找到的一个觉得用来在程序中进行Activity之间切换的很好的一个例子:

  1. package com.asai.android.demo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.Button;
  8. import android.widget.Spinner;
  9. public class demoMain extends Activity {
  10. private Button btnDemo;
  11. private Spinner sprAnim;
  12. /** Called when the activity is first created. */
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. btnDemo = (Button) findViewById(R.id.btnDemo);
  18. sprAnim = (Spinner) findViewById(R.id.sprAnim);
  19. //Spinner的内容适配器
  20. ArrayAdapter<?> animType = ArrayAdapter.createFromResource(this,
  21. R.array.anim_type, android.R.layout.simple_spinner_item);
  22. animType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  23. sprAnim.setAdapter(animType);
  24. //默认选中第一项
  25. sprAnim.setSelection(0);
  26. btnDemo.setOnClickListener(new View.OnClickListener() {
  27. public void onClick(View view) {
  28. startActivity(new Intent(demoMain.this, demoDetail.class));
  29. switch (sprAnim.getSelectedItemPosition()) {
  30. case 1:
  31. //淡入淡出效果 
  32. overridePendingTransition(R.anim.fade, R.anim.hold);
  33. break;
  34. case 2:
  35. overridePendingTransition(R.anim.alpha_rotate,
  36. R.anim.my_alpha_action);
  37. break;
  38. case 3:
  39. overridePendingTransition(R.anim.alpha_scale_rotate,
  40. R.anim.my_alpha_action);
  41. break;
  42. case 4:
  43. overridePendingTransition(
  44. R.anim.alpha_scale_translate_rotate,
  45. R.anim.my_alpha_action);
  46. break;
  47. case 5:
  48. overridePendingTransition(R.anim.alpha_scale_translate,
  49. R.anim.my_alpha_action);
  50. break;
  51. case 6:
  52. //中心放大效果
  53. overridePendingTransition(R.anim.alpha_scale,
  54. R.anim.my_alpha_action);
  55. break;
  56. case 7:
  57. overridePendingTransition(R.anim.alpha_translate_rotate,
  58. R.anim.my_alpha_action);
  59. break;
  60. case 8:
  61. overridePendingTransition(R.anim.alpha_translate,
  62. R.anim.my_alpha_action);
  63. break;
  64. case 9:
  65. overridePendingTransition(R.anim.my_rotate_action,
  66. R.anim.my_alpha_action);
  67. break;
  68. case 10:
  69. overridePendingTransition(R.anim.my_scale_action,
  70. R.anim.my_alpha_action);
  71. break;
  72. case 11:
  73. overridePendingTransition(R.anim.my_translate_action,
  74. R.anim.my_alpha_action);
  75. break;
  76. case 12:
  77. overridePendingTransition(R.anim.myanimation_simple,
  78. R.anim.my_alpha_action);
  79. break;
  80. case 13:
  81. overridePendingTransition(R.anim.myown_design,
  82. R.anim.my_alpha_action);
  83. break;
  84. case 14:
  85. overridePendingTransition(R.anim.scale_rotate,
  86. R.anim.my_alpha_action);
  87. break;
  88. case 15:
  89. overridePendingTransition(R.anim.scale_translate_rotate,
  90. R.anim.my_alpha_action);
  91. break;
  92. case 16:
  93. overridePendingTransition(R.anim.scale_translate,
  94. R.anim.my_alpha_action);
  95. break;
  96. case 17:
  97. overridePendingTransition(R.anim.translate_rotate,
  98. R.anim.my_alpha_action);
  99. break;
  100. case 18:
  101. overridePendingTransition(R.anim.hyperspace_in,
  102. R.anim.hyperspace_out);
  103. break;
  104. case 19:
  105. overridePendingTransition(R.anim.shake,
  106. R.anim.my_alpha_action);
  107. break;
  108. case 20:
  109. overridePendingTransition(R.anim.push_left_in,
  110. R.anim.push_left_out);
  111. break;
  112. case 21:
  113. //从下方移出效果 overridePendingTransition(R.anim.push_up_in,
  114. R.anim.push_up_out);
  115. break;
  116. case 22:
  117. overridePendingTransition(R.anim.slide_left,
  118. R.anim.slide_right);
  119. break;
  120. case 23:
  121. overridePendingTransition(R.anim.slide_top_to_bottom,
  122. R.anim.my_alpha_action);
  123. break;
  124. case 24:
  125. overridePendingTransition(R.anim.wave_scale,
  126. R.anim.my_alpha_action);
  127. break;
  128. }
  129. }
  130. });
  131. }
  132. }
复制代码



另外一个activity可以自己随便写个,这里还用到了就是arrays.xml作为Spinner的适配器: 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="anim_type">
  4. <item>zoom_enter</item>
  5. <item>fade</item>
  6. <item>alpha_rotate</item>
  7. <item>alpha_scale_rotate</item>
  8. <item>alpha_scale_translate_rotate</item>
  9. <item>alpha_scale_translate</item>
  10. <item>alpha_scale</item>
  11. <item>alpha_translate_rotate</item>
  12. <item>alpha_translate</item>
  13. <item>my_rotate_action</item>
  14. <item>my_scale_action</item>
  15. <item>my_translate_action</item>
  16. <item>myanimation_simple</item>
  17. <item>myown_design</item>
  18. <item>scale_rotate</item>
  19. <item>scale_translate_rotate</item>
  20. <item>scale_translate</item>
  21. <item>translate_rotate</item>
  22. <item>hyperspace_in</item>
  23. <item>shake</item>
  24. <item>push_left_in</item>
  25. <item>push_up_in</item>
  26. <item>slide_left</item>
  27. <item>slide_top_to_bottom</item>
  28. <item>wave_scale</item>
  29. </string-array>
  30. </resources>
复制代码



其实这个Demo最重要的就是anim中的一些xml动画定义: 
下面我一个一个的来介绍下(我觉得实用性很好的): 
push_up_in.xml: 

  1. <set xmlns:android="http://schemas.android.com/apk/res/android">
  2. <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300"/>
  3. <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
  4. </set>
复制代码



相信这段代码我不用解释应该也看的懂:改变y坐标来实现动画效果; 
相对应的就是: 
push_up_out.xml: 

  1. <set xmlns:android="http://schemas.android.com/apk/res/android">
  2. <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="300"/>
  3. <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
  4. </set>
复制代码

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=49969

posted @ 2012-01-10 18:47  灰太狼_lilongmin  阅读(1950)  评论(0编辑  收藏  举报