自定义控件之仿优酷菜单

1.布局文件

<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/menu3"
        android:layout_width="280dp"
        android:layout_height="140dp"
        android:background="@drawable/level3"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        >
        <!-- 0 -->
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel1"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="12dp"
            android:layout_marginBottom="5dp"
            />
         <!-- 1 -->
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel2"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="35dp"
            android:layout_marginBottom="55dp"
            />
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel3"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="67dp"
            android:layout_marginBottom="87dp"
            />
        
         <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel4"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="6dp"
            />
         
         <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel5"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="67dp"
            android:layout_marginBottom="87dp"
            />
         
         <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel6"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="35dp"
            android:layout_marginBottom="55dp"
            />
         
         <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/channel7"
            android:layout_alignParentBottom="true"
             android:layout_alignParentRight="true"
            android:layout_marginRight="12dp"
            android:layout_marginBottom="5dp"
            />
        
    </RelativeLayout>
    
    <RelativeLayout 
         android:id="@+id/menu2"
        android:layout_width="180dp"
        android:layout_height="90dp"
        android:background="@drawable/level2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_search"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="12dp"
            android:layout_marginBottom="5dp"
            />
        
        <Button 
            android:id="@+id/menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_menu"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="6dp"
            />
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_myyouku"
            android:layout_alignParentBottom="true"
             android:layout_alignParentRight="true"
            android:layout_marginRight="12dp"
            android:layout_marginBottom="5dp"
            />
        
    </RelativeLayout>

    
    <RelativeLayout 
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@drawable/level1"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        >
        
        <Button 
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/icon_home"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dp"
            />
        
    </RelativeLayout>
    
</RelativeLayout>

2.activity

public class MainActivity extends Activity implements OnClickListener{

    private Button mHome;
	private Button mMenu;
	/**
	 * 外部菜单显示隐藏的标示
	 */
	private boolean isShowMenu3 = true;
	/**
	 * 中部菜单显示隐藏的标示
	 */
	private boolean isShowMenu2 = true;
	private RelativeLayout mMenu3;
	private RelativeLayout mMenu2;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initView();
    }
    /**
     * 初始化控件的
     * 2016-8-12 上午10:11:18
     */
	private void initView() {
		mHome = (Button) findViewById(R.id.home);
		mMenu = (Button) findViewById(R.id.menu);
		mMenu2 = (RelativeLayout) findViewById(R.id.menu2);
		mMenu3 = (RelativeLayout) findViewById(R.id.menu3);
		
		//设置按钮的点击事件
		mHome.setOnClickListener(this);
		mMenu.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		//getId : 获取被点击的按钮的id
		switch (v.getId()) {
		case R.id.menu:
			
			//判断是否需要执行另一个动画
			if (Tool.isAnimation()) {
				return;
			}
			
			//如果外部菜单显示,点击隐藏
			//如果外部菜单隐藏,点击显示
			//需要知道外部菜单的显示状态
			if (isShowMenu3) {
				//显示 -> 隐藏
				//isShowMenu3 = !isShowMenu3;
				Tool.hide(mMenu3,0l);
			}else{
				//隐藏 -> 显示
				//isShowMenu3 = !isShowMenu3;
				Tool.show(mMenu3);
			}
			isShowMenu3 = !isShowMenu3;
			break;

		case R.id.home:
			
			//判断是否需要执行另一个动画
			if (Tool.isAnimation()) {
				return;
			}
			
			//如果外部菜单和中部菜单显示,点击全部隐藏
			//如果外部菜单隐藏,中部菜单显示,点击隐藏中部菜单
			//如果外部菜单和中部菜单全部隐藏,点击显示中部菜单
			if (isShowMenu3) {
				//如果外部菜单和中部菜单显示,点击全部隐藏
				isShowMenu3 = false;
				//isShowMenu2 = false;
				Tool.hide(mMenu3,0l);
				Tool.hide(mMenu2,300l);
			}else if(isShowMenu2){
				//如果外部菜单隐藏,中部菜单显示,点击隐藏中部菜单
				//isShowMenu2 = false;
				Tool.hide(mMenu2,0l);
			}else{
				//如果外部菜单和中部菜单全部隐藏,点击显示中部菜单
				//isShowMenu2 = true;
				Tool.show(mMenu2);
			}
			isShowMenu2 = !isShowMenu2;
			break;
		}
	}   
}
3.定义的工具类  里面主要是 动画的显示和 隐藏 和 布局中按钮的是否可以点击处理

public class Tool {
	
	/**
	 * 标示动画是否执行完毕,如果大于0,动画正在执行,不能执行另一个动画,如果等于小于0,动画执行完毕可以执行另一个动画
	 */
	private static int isStart;
	
	/**
	 * 隐藏操作
	 * 以view作为参数,原因:提供复用性,这样,之后传递任何控件都可以实现隐藏操作,不用在来修改工具类,面向父类的开发
	 * 2016-8-12 上午10:26:04
	 */
	public static void hide(View menu,long startOffset){
		//旋转动画,0,-180
		//隐藏外部菜单的时候是没有延迟时间的,只有在外部菜单和中部菜单一起隐藏的时候,才有延迟时间
		setAnimation(menu,0,-180,startOffset);
		//禁止菜单和按钮的点击事件
		setClickable(menu,false);
	}
	/**
	 * 显示操作
	 * 2016-8-12 上午10:28:24
	 */
	public static void show(View menu){
		//旋转动画,-180,0
		setAnimation(menu,-180,0,0l);
		setClickable(menu,true);
	}
	
	/**
	 * 隐藏显示旋转动画
	 * 2016-8-12 上午10:30:01
	 */
	private static void setAnimation(View menu,float fromDegrees,float toDegrees,long startOffset){
		//fromDegrees : 从哪里开始旋转
		//toDegrees : 旋转到哪里
		//pivotXType : 旋转的类型,以什么作为参照物旋转Animation.ABSOLUTE:绝对位置,Animation.RELATIVE_TO_SELF:以自身,Animation.RELATIVE_TO_PARENT:以父控件
		//pivotXValue : 旋转的x的坐标
		RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
		animation.setDuration(500);//动画的持续时间
		animation.setFillAfter(true);//保持动画结束的状态
		
		animation.setStartOffset(startOffset);//设置动画开始执行的时间,延迟时间,单位毫秒
		
		//监听动画的实现操作
		animation.setAnimationListener(listener);
		
		
		menu.startAnimation(animation);//执行动画的操作
		
	}
	
	/**
	 * 动画的监听
	 */
	private static AnimationListener listener = new AnimationListener() {
		//动画开始调用的方法
		@Override
		public void onAnimationStart(Animation animation) {
			isStart++;
		}
		//动画重复执行调用的方法
		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		//动画结束调用的方法
		@Override
		public void onAnimationEnd(Animation animation) {
			isStart--;
		}
	};
	/**
	 * 判断动画是否执行
	 * @return  true:执行,不能执行另一个动画;false:不执行,执行另一个动画
	 * 2016-8-12 上午11:21:12
	 */
	public static boolean isAnimation(){
		return isStart > 0;
	}
	
	/**
	 * 设置菜单和按钮是否可以点击
	 * @param
	 * 		menu:设置的菜单
	 * 		b : 是否可以点击
	 * 2016-8-12 上午10:53:48
	 */
	private static void setClickable(View menu, boolean b) {
		menu.setEnabled(b);//设置菜单是否可用,false:不可用,不可以做任何操作,true:可用
		//不仅仅是禁止菜单点击事件,还要禁止菜单中按钮的点击事件
		//instanceof : 判断控件是否是ViewGroup
		if (menu instanceof ViewGroup) {
			ViewGroup viewGroup = (ViewGroup)menu;
			//viewGroup.getChildCount();//获取菜单中的子控件的个数
			for (int i = 0; i < viewGroup.getChildCount(); i++) {
				View view = viewGroup.getChildAt(i);//根据子控件布局文件中的角标,获取子控件的对象,角标:布局文件中从上往下,从0开始往后
				//设置子控件不可用
				view.setEnabled(b);
			}
		}
	}	
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值