歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 高仿【優酷】圓盤旋轉菜單 的實現

Android 高仿【優酷】圓盤旋轉菜單 的實現

日期:2017/3/1 10:12:01   编辑:Linux編程

目前,用戶對安卓應用程序的UI設計要求越來越高,因此,掌握一些新穎的設計很有必要.

比如菜單,傳統的菜單已經不能滿足用戶的需求. 其中優酷中圓盤旋轉菜單的實現就比較好.

該菜單共分裡外三層導航菜單.可以依次從外向裡關閉三層菜單,也可以反向打開,並且伴有圓盤旋轉的動畫效果。

首先,看下效果:

以下是具體的代碼及解釋:

1. 菜單布局文件:

大家看到主要有三個RalativeLayout,就是大家看到的三層,但是關於圖片的傾斜 是怎樣實現的呢?實際上是個假象,圖片是正放的,裡面圖像是傾斜的。

  1. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" >
  5. <RelativeLayout
  6. android:layout_width="100dip"
  7. android:layout_height="50dip"
  8. android:layout_alignParentBottom="true"
  9. android:layout_centerHorizontal="true"
  10. android:background="@drawable/level1" >
  11. <ImageButton
  12. android:id="@+id/home"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_centerInParent="true"
  16. android:background="@drawable/icon_home" />
  17. </RelativeLayout>
  18. <RelativeLayout
  19. android:layout_width="180dip"
  20. android:layout_height="90dip"
  21. android:layout_alignParentBottom="true"
  22. android:layout_centerHorizontal="true"
  23. android:id="@+id/level2"
  24. android:background="@drawable/level2" >
  25. <ImageButton
  26. android:id="@+id/search"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_alignParentBottom="true"
  30. android:layout_margin="10dip"
  31. android:background="@drawable/icon_search" />
  32. <ImageButton
  33. android:id="@+id/menu"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_centerHorizontal="true"
  37. android:layout_margin="6dip"
  38. android:background="@drawable/icon_menu" />
  39. <ImageButton
  40. android:id="@+id/myyouku"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_alignParentBottom="true"
  44. android:layout_alignParentRight="true"
  45. android:layout_margin="10dip"
  46. android:background="@drawable/icon_myyouku" />
  47. </RelativeLayout>
  48. <RelativeLayout
  49. android:layout_width="280dip"
  50. android:layout_height="140dip"
  51. android:layout_alignParentBottom="true"
  52. android:layout_centerHorizontal="true"
  53. android:id="@+id/level3"
  54. android:background="@drawable/level3" >
  55. <ImageButton
  56. android:id="@+id/c1"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_alignParentBottom="true"
  60. android:layout_marginBottom="6dip"
  61. android:layout_marginLeft="12dip"
  62. android:background="@drawable/channel1" />
  63. <ImageButton
  64. android:id="@+id/c2"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:layout_above="@id/c1"
  68. android:layout_marginBottom="12dip"
  69. android:layout_marginLeft="28dip"
  70. android:background="@drawable/channel2" />
  71. <ImageButton
  72. android:id="@+id/c3"
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:layout_above="@id/c2"
  76. android:layout_marginBottom="6dip"
  77. android:layout_marginLeft="8dip"
  78. android:layout_toRightOf="@id/c2"
  79. android:background="@drawable/channel3" />
  80. <ImageButton
  81. android:id="@+id/c4"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:layout_centerHorizontal="true"
  85. android:layout_margin="6dip"
  86. android:background="@drawable/channel4" />
  87. <ImageButton
  88. android:id="@+id/c5"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:layout_above="@+id/c6"
  92. android:layout_marginBottom="6dip"
  93. android:layout_marginRight="8dip"
  94. android:layout_toLeftOf="@+id/c6"
  95. android:background="@drawable/channel5" />
  96. <ImageButton
  97. android:id="@+id/c6"
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:layout_above="@+id/c7"
  101. android:layout_marginBottom="12dip"
  102. android:layout_marginRight="28dip"
  103. android:layout_alignParentRight="true"
  104. android:background="@drawable/channel6" />
  105. <ImageButton
  106. android:id="@+id/c7"
  107. android:layout_width="wrap_content"
  108. android:layout_height="wrap_content"
  109. android:layout_alignParentBottom="true"
  110. android:layout_marginBottom="6dip"
  111. android:layout_marginRight="12dip"
  112. android:layout_alignParentRight="true"
  113. android:background="@drawable/channel7" />
  114. </RelativeLayout>
  115. </RelativeLayout>

2. MainActivity;

  1. import android.os.Bundle;
  2. import android.app.Activity;
  3. import android.view.Menu;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.ImageButton;
  7. import android.widget.RelativeLayout;
  8. public class MainActivity extends Activity {
  9. private ImageButton home;
  10. private ImageButton menu;
  11. private RelativeLayout level2;
  12. private RelativeLayout level3;
  13. private boolean isLevel2Show = true;
  14. private boolean isLevel3Show = true;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. home = (ImageButton) findViewById(R.id.home);
  20. menu = (ImageButton) findViewById(R.id.menu);
  21. level2 = (RelativeLayout) findViewById(R.id.level2);
  22. level3 = (RelativeLayout) findViewById(R.id.level3);
  23. menu.setOnClickListener(new OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. if(isLevel3Show){
  27. //隱藏3級導航菜單
  28. MyAnimation.startAnimationOUT(level3, 500, 0);
  29. }else {
  30. //顯示3級導航菜單
  31. MyAnimation.startAnimationIN(level3, 500);
  32. }
  33. isLevel3Show = !isLevel3Show;
  34. }
  35. });
  36. home.setOnClickListener(new OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. if(!isLevel2Show){
  40. //顯示2級導航菜單
  41. MyAnimation.startAnimationIN(level2, 500);
  42. } else {
  43. if(isLevel3Show){
  44. //隱藏3級導航菜單
  45. MyAnimation.startAnimationOUT(level3, 500, 0);
  46. //隱藏2級導航菜單
  47. MyAnimation.startAnimationOUT(level2, 500, 500);
  48. isLevel3Show = !isLevel3Show;
  49. }
  50. else {
  51. //隱藏2級導航菜單
  52. MyAnimation.startAnimationOUT(level2, 500, 0);
  53. }
  54. }
  55. isLevel2Show = !isLevel2Show;
  56. }
  57. });
  58. }
  59. }

3. 自定義動畫類MyAnimation:

  1. import android.view.View;
  2. import android.view.ViewGroup;
  3. import android.view.animation.Animation;
  4. import android.view.animation.Animation.AnimationListener;
  5. import android.view.animation.RotateAnimation;
  6. public class MyAnimation {
  7. //入動畫
  8. public static void startAnimationIN(ViewGroup viewGroup, int duration){
  9. for(int i = 0; i < viewGroup.getChildCount(); i++ ){
  10. viewGroup.getChildAt(i).setVisibility(View.VISIBLE);//設置顯示
  11. viewGroup.getChildAt(i).setFocusable(true);//獲得焦點
  12. viewGroup.getChildAt(i).setClickable(true);//可以點擊
  13. }
  14. Animation animation;
  15. /**
  16. * 旋轉動畫
  17. * RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
  18. * fromDegrees 開始旋轉角度
  19. * toDegrees 旋轉到的角度
  20. * pivotXType X軸 參照物
  21. * pivotXValue x軸 旋轉的參考點
  22. * pivotYType Y軸 參照物
  23. * pivotYValue Y軸 旋轉的參考點
  24. */
  25. animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
  26. animation.setFillAfter(true);//停留在動畫結束位置
  27. animation.setDuration(duration);
  28. viewGroup.startAnimation(animation);
  29. }
  30. //出動畫
  31. public static void startAnimationOUT(final ViewGroup viewGroup, int duration , int startOffSet){
  32. Animation animation;
  33. animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
  34. animation.setFillAfter(true);//停留在動畫結束位置
  35. animation.setDuration(duration);
  36. animation.setStartOffset(startOffSet);
  37. animation.setAnimationListener(new AnimationListener() {
  38. @Override
  39. public void onAnimationStart(Animation animation) {
  40. // TODO Auto-generated method stub
  41. }
  42. @Override
  43. public void onAnimationRepeat(Animation animation) {
  44. // TODO Auto-generated method stub
  45. }
  46. @Override
  47. public void onAnimationEnd(Animation animation) {
  48. for(int i = 0; i < viewGroup.getChildCount(); i++ ){
  49. viewGroup.getChildAt(i).setVisibility(View.GONE);//設置顯示
  50. viewGroup.getChildAt(i).setFocusable(false);//獲得焦點
  51. viewGroup.getChildAt(i).setClickable(false);//可以點擊
  52. }
  53. }
  54. });
  55. viewGroup.startAnimation(animation);
  56. }
  57. }

這樣,一個高仿優酷三級導航圓盤旋轉菜單就完成了.,以後完全可以借鑒這些優秀的UI設計,甚至根據新的需求,可以做出更好的UI。

Copyright © Linux教程網 All Rights Reserved