Android 高仿优酷旋转菜单

这是一个很早版本的优酷菜单,效果挺不错的,实现起来也挺简单的。废话不说,直接上代码:
首先是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链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值