Android 高仿【优酷】圆盘旋转菜单的实现

jopen 11年前

目前,用户对安卓应用程序的UI设计要求越来越高,因此,掌握一些新颖的设计很有必要.

比如菜单,传统的菜单已经不能满足用户的需求. 其中优酷中圆盘旋转菜单的实现就比较优秀,这里我提供下我的思路及实现,仅供参考.


不过在这里个人认为点击home图标关闭菜单的时候先关第三层,接着第二层,效果比较好看点,只需在关闭第二层的时候加个延迟便可。


该菜单共分里外三层导航菜单.可以依次从外向里关闭三层菜单,也可以反向打开,并且伴有圆盘旋转的动画效果

首先,看下效果:

1345560978_8709.jpg

以下是具体的代码及解释:

1. 菜单布局文件:

大家看到主要有三个RalativeLayout,就是大家看到的三层,但是关于图片的倾斜 是怎样实现的呢?实际上是个假象,图片是正放的,里面图像是倾斜的


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

[java] view plaincopy
</div> </div>
  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.       
  9. public class MainActivity extends Activity {      
  10.       
  11.     private ImageButton home;      
  12.     private ImageButton menu;      
  13.     private RelativeLayout level2;      
  14.     private RelativeLayout level3;      
  15.           
  16.     private boolean isLevel2Show = true;      
  17.     private boolean isLevel3Show = true;      
  18.       
  19.     @Override      
  20.     public void onCreate(Bundle savedInstanceState) {      
  21.         super.onCreate(savedInstanceState);      
  22.         setContentView(R.layout.activity_main);      
  23.       
  24.         home = (ImageButton) findViewById(R.id.home);      
  25.         menu = (ImageButton) findViewById(R.id.menu);      
  26.       
  27.         level2 = (RelativeLayout) findViewById(R.id.level2);      
  28.         level3 = (RelativeLayout) findViewById(R.id.level3);      
  29.       
  30.         menu.setOnClickListener(new OnClickListener() {      
  31.       
  32.             @Override      
  33.             public void onClick(View v) {      
  34.                 if(isLevel3Show){      
  35.                     //隐藏3级导航菜单      
  36.                     MyAnimation.startAnimationOUT(level3, 5000);      
  37.                 }else {      
  38.                     //显示3级导航菜单      
  39.                     MyAnimation.startAnimationIN(level3, 500);      
  40.                 }      
  41.                       
  42.                 isLevel3Show = !isLevel3Show;      
  43.             }      
  44.         });      
  45.       
  46.         home.setOnClickListener(new OnClickListener() {      
  47.       
  48.             @Override      
  49.             public void onClick(View v) {      
  50.                 if(!isLevel2Show){      
  51.                     //显示2级导航菜单      
  52.                     MyAnimation.startAnimationIN(level2, 500);      
  53.                 } else {      
  54.                     if(isLevel3Show){      
  55.                         //隐藏3级导航菜单      
  56.                         MyAnimation.startAnimationOUT(level3, 5000);      
  57.                         //隐藏2级导航菜单      
  58.                         MyAnimation.startAnimationOUT(level2, 500500);      
  59.                         isLevel3Show = !isLevel3Show;      
  60.                     }      
  61.                     else {      
  62.                         //隐藏2级导航菜单      
  63.                         MyAnimation.startAnimationOUT(level2, 5000);      
  64.                     }      
  65.                 }      
  66.                 isLevel2Show = !isLevel2Show;      
  67.             }      
  68.         });      
  69.       
  70.     }      
  71.       
  72. }      
</div>
3. 自定义动画类MyAnimation:

[java] view plaincopy
</div> </div>
  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.       
  7. public class MyAnimation {      
  8.     //入动画      
  9.     public static void startAnimationIN(ViewGroup viewGroup, int duration){      
  10.         for(int i = 0; i < viewGroup.getChildCount(); i++ ){      
  11.             viewGroup.getChildAt(i).setVisibility(View.VISIBLE);//设置显示      
  12.             viewGroup.getChildAt(i).setFocusable(true);//获得焦点      
  13.             viewGroup.getChildAt(i).setClickable(true);//可以点击      
  14.         }      
  15.               
  16.         Animation animation;      
  17.         /**   
  18.          * 旋转动画   
  19.          * RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)   
  20.          * fromDegrees 开始旋转角度   
  21.          * toDegrees 旋转到的角度   
  22.          * pivotXType X轴 参照物   
  23.          * pivotXValue x轴 旋转的参考点   
  24.          * pivotYType Y轴 参照物   
  25.          * pivotYValue Y轴 旋转的参考点   
  26.          */      
  27.         animation = new RotateAnimation(-1800, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);      
  28.         animation.setFillAfter(true);//停留在动画结束位置      
  29.         animation.setDuration(duration);      
  30.               
  31.         viewGroup.startAnimation(animation);      
  32.               
  33.     }      
  34.           
  35.     //出动画      
  36.     public static void startAnimationOUT(final ViewGroup viewGroup, int duration , int startOffSet){      
  37.         Animation animation;      
  38.         animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);      
  39.         animation.setFillAfter(true);//停留在动画结束位置      
  40.         animation.setDuration(duration);      
  41.         animation.setStartOffset(startOffSet);      
  42.         animation.setAnimationListener(new AnimationListener() {      
  43.                   
  44.             @Override      
  45.             public void onAnimationStart(Animation animation) {      
  46.                 // TODO Auto-generated method stub      
  47.                       
  48.             }      
  49.                   
  50.             @Override      
  51.             public void onAnimationRepeat(Animation animation) {      
  52.                 // TODO Auto-generated method stub      
  53.                       
  54.             }      
  55.                   
  56.             @Override      
  57.             public void onAnimationEnd(Animation animation) {      
  58.                 for(int i = 0; i < viewGroup.getChildCount(); i++ ){      
  59.                     viewGroup.getChildAt(i).setVisibility(View.GONE);//设置显示      
  60.                     viewGroup.getChildAt(i).setFocusable(false);//获得焦点      
  61.                     viewGroup.getChildAt(i).setClickable(false);//可以点击      
  62.                 }      
  63.                       
  64.             }      
  65.         });      
  66.               
  67.         viewGroup.startAnimation(animation);      
  68.     }      
  69. }      
</div>
这样,一个高仿优酷三级导航圆盘旋转菜单就完成了.,以后完全可以借鉴这些优秀的UI设计,甚至根据新的需求,可以做出更好的UI.