这是一个很早版本的优酷菜单,效果挺不错的,实现起来也挺简单的。废话不说,直接上代码:
首先是xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--一级菜单-->
<RelativeLayout
android:id="@+id/level1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level1">
<ImageView
android:id="@+id/id_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/icon_home" />
</RelativeLayout>
<!--二级菜单-->
<RelativeLayout
android:id="@+id/level2"
android:layout_width="180dp"
android:layout_height="88dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:src="@drawable/icon_search" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:src="@drawable/icon_myyouku" />
<ImageView
android:id="@+id/id_menu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:src="@drawable/icon_menu" />
</RelativeLayout>
<!--三级菜单-->
<RelativeLayout
android:id="@+id/level3"
android:layout_width="280dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level3">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:background="@drawable/channel1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="6dp"
android:layout_marginRight="6dp"
android:background="@drawable/channel5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="45dp"
android:layout_marginLeft="28dp"
android:background="@drawable/channel2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="45dp"
android:layout_marginRight="28dp"
android:background="@drawable/channel6" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="75dp"
android:layout_marginLeft="60dp"
android:background="@drawable/channel3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="75dp"
android:layout_marginRight="60dp"
android:background="@drawable/channel7" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/channel4" />
</RelativeLayout>
</RelativeLayout>
静态效果如下:
接下来实现关闭:
public static int animCount=0;//记录当初动画数量;
public static void closeMenu(RelativeLayout layout,int starttime) {
//处理 菜单隐藏后,view还可以被点击的bug
for (int i=0;i<layout.getChildCount();i++){
layout.getChildAt(i).setEnabled(false);
}
RotateAnimation rotateAnimation = new RotateAnimation(0, -180,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1f);
rotateAnimation.setDuration(500);
rotateAnimation.setFillAfter(true);
rotateAnimation.setStartOffset(starttime);
rotateAnimation.setAnimationListener(new MyAnimationListeren());
layout.startAnimation(rotateAnimation);
}
public static void openMenu(RelativeLayout layout,int starttime) {
//处理 菜单显示后,view还可以被点击恢复
for (int i=0;i<layout.getChildCount();i++){
layout.getChildAt(i).setEnabled(true);
}
RotateAnimation rotateAnimation = new RotateAnimation(-180, 0,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,1f);
rotateAnimation.setDuration(500);
rotateAnimation.setFillAfter(true);
rotateAnimation.setStartOffset(starttime);
rotateAnimation.setAnimationListener(new MyAnimationListeren());
layout.startAnimation(rotateAnimation);
}
static class MyAnimationListeren implements Animation.AnimationListener{
@Override
public void onAnimationStart(Animation animation) {
animCount++;
}
@Override
public void onAnimationEnd(Animation animation) {
animCount--;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
最后是activity中的实现:
public void onClick(View v) {
switch (v.getId()) {
case R.id.id_menu:
if (AnimUtil.animCount!=0){
//当前有动画在执行,直接返回,不进行操作
return;
}
if (ifLevel2Show) {
//需要隐藏
int starttime=0;
if (ifLevel3Show) {
AnimUtil.closeMenu(layoutTHLevel,starttime);
ifLevel3Show = !ifLevel3Show;
starttime+=200; }
AnimUtil.closeMenu(layoutTLevel,starttime);
} else {
//需要显示
AnimUtil.openMenu(layoutTLevel); }
ifLevel2Show = !ifLevel2Show;
break;
case R.id.id_menu2:
if (AnimUtil.animCount!=0){
//当前有动画在执行,直接返回,不进行操作
return;
}
if (ifLevel3Show) {
//需要隐藏
AnimUtil.closeMenu(layoutTHLevel,0);
} else {
//需要显示
AnimUtil.openMenu(layoutTHLevel);
} ifLevel3Show = !ifLevel3Show; break; }}
demo链接:高仿优酷旋转菜单demo链接