Android 自定义控件初级 多层按钮旋转控件

 类似于<img src="https://img-blog.csdn.net/20150706144705531?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><img src="https://img-blog.csdn.net/20150706144722417?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

首先是布局文件如下:
</p><p></p><pre name="code" class="html"><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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <!-- 第一级导航菜单  -->
    <RelativeLayout
      android:id="@+id/firstlevel"
      android:layout_width="100dp"
      android:layout_height="50dp"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:background="@drawable/level1">
               
         <ImageButton 
             android:id="@+id/btnhome"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:background="@drawable/icon_home"
             android:layout_centerInParent="true"
          /> 
        
    </RelativeLayout>
    
    <!-- 第二级导航菜单  -->
     <RelativeLayout
      android:id="@+id/secondlevel"
      android:layout_width="180dp"
      android:layout_height="90dp"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:background="@drawable/level2">
               
         <ImageButton 
             android:id="@+id/search"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:background="@drawable/icon_search"
             android:layout_alignParentBottom="true"
             android:layout_marginLeft="9dp"
             android:layout_marginBottom="6dp"
          /> 
        
          <ImageButton 
              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="10dp"
          />
          
                 
         <ImageButton 
             android:id="@+id/myyouku"
             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="9dp"
             android:layout_marginBottom="6dp"
          /> 
          
    </RelativeLayout>
    
     
     <!-- 第三级导航菜单  -->
     <RelativeLayout 
         android:id="@+id/thirdevel"
         android:layout_width="280dp"
         android:layout_height="140dp"
         android:background="@drawable/level3"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true">
         
         <ImageButton 
            android:id="@+id/chanel1"
            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="6dp"
          />
         
          <ImageButton 
            android:id="@+id/chanel2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"   
            android:background="@drawable/channel2" 
            android:layout_above="@id/chanel1"
            android:layout_marginLeft="30dp"
            android:layout_marginBottom="12dp"
          />
          <ImageButton 
            android:id="@+id/chanel3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"   
            android:background="@drawable/channel3" 
            android:layout_above="@id/chanel2"
            android:layout_marginLeft="70dp"
            android:layout_marginBottom="12dp"
          />
           <ImageButton 
            android:id="@+id/chanel4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"   
            android:background="@drawable/channel4" 
            android:layout_above="@id/chanel3"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="12dp"
            
          />
           
            <ImageButton 
            android:id="@+id/chanel5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"   
            android:background="@drawable/channel5" 
            android:layout_above="@+id/chanel6"
            android:layout_alignParentRight="true"
            android:layout_marginRight="70dp"
            android:layout_marginBottom="12dp"
          />
       
            <ImageButton 
            android:id="@+id/chanel6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"   
            android:background="@drawable/channel6" 
            android:layout_above="@+id/chanel7"
            android:layout_alignParentRight="true"
            android:layout_marginRight="30dp"
            android:layout_marginBottom="12dp"
          />
       
           <ImageButton 
            android:id="@+id/chanel7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"   
            android:background="@drawable/channel7" 
            android:layout_above="@id/chanel7"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="12dp"
            android:layout_marginBottom="6dp"
          />
      
     </RelativeLayout>
    

</RelativeLayout>
然后逻辑代码如下:

package com.ShaoZhongqi.LessonUi;

import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

	private ImageButton imgbtnsecondmenu;
	private ImageButton imgbtnhome;
	
	//找到布局文件 
	private RelativeLayout secondlevellayout;
	private RelativeLayout thirdlevellayout;
	
	//控制变量
	private boolean isShowSecondLevel = true;
	private boolean isShowThirdLevel = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d("shao","first haha");
		
		//主home按钮  和 二级菜单按钮   
		imgbtnsecondmenu = (ImageButton) findViewById(R.id.menu);
		imgbtnhome = (ImageButton)findViewById(R.id.btnhome);
		
		//布局视图 
		secondlevellayout = (RelativeLayout)findViewById(R.id.secondlevel);
		thirdlevellayout = (RelativeLayout)findViewById(R.id.thirdevel);
		
		imgbtnsecondmenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Log.d("shao","first menu1");
				
				//二级控制按钮  控制第三层布局文件的显示或者隐藏 
				if(isShowThirdLevel)
				{
					//第三层view显示-->隐藏 
					MyAnimation.HideView(thirdlevellayout, 500, 0);
					Log.d("shao","first menu2");
					
				}else
				{
					
					//第三层view隐藏---->显示 
					MyAnimation.ShowView(thirdlevellayout, 500);
					
					
				}
				
				isShowThirdLevel = !isShowThirdLevel;
					
			}
		});
		
		
		//主home键控制第二层 和 第三层的显示 
		imgbtnhome.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(isShowSecondLevel)
				{
					
					if(isShowThirdLevel)
					{
						
						//二级和三级菜单都显示  那就隐藏二级和三级菜单  显示---->隐藏   显示-->隐藏 
						//需要延迟
						MyAnimation.HideView(thirdlevellayout, 500, 0);
						MyAnimation.HideView(secondlevellayout, 500, 1000);
						isShowThirdLevel = !isShowThirdLevel;
						Log.d("shao","first menu4");
						
					}else
					{
						//二级显示 三级不显示的情况   二级显示-->隐藏
						MyAnimation.HideView(secondlevellayout, 500, 0);
							
					}
					
									
				}else
				{
					
					//二级隐藏--->显示 
					MyAnimation.ShowView(secondlevellayout, 500);
					
				}
				
				isShowSecondLevel = !isShowSecondLevel;
				
			}
		});
				
	}


	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

旋转动画 MyAnimation.java的代码如下:

package com.ShaoZhongqi.LessonUi;

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;

public class MyAnimation {
	
	//入动画   控制view的显示 
	public static void   ShowView(ViewGroup group,long duration)
	{
		 for(int i = 0 ; i < group.getChildCount();i++)
		 {
			  View childview = group.getChildAt(i);
			  childview.setVisibility(View.VISIBLE);
			  childview.setLongClickable(true);  //可以点击 
			  childview.setFocusable(true); //可以获得焦点
			 
		 }
		 
		 //设置旋转动画 
         RotateAnimation animation = new RotateAnimation(-180 ,0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
         animation.setFillAfter(true);//停留在动画结束的位置
         animation.setDuration(duration);
		 group.setAnimation(animation);
	}
	
	public static void HideView(final ViewGroup viewGroup, int duration, int startOffSet)
	{

		Animation animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
		animation.setFillAfter(true);// 停留在 动画运行结束的位置
		animation.setDuration(duration);// 动画运行时间
		animation.setStartOffset(startOffSet);// 动画延迟启动时间
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				for (int i = 0; i < viewGroup.getChildCount(); i++) {
					viewGroup.getChildAt(i).setVisibility(View.GONE);
					viewGroup.getChildAt(i).setClickable(false);// 不可以点击
					viewGroup.getChildAt(i).setFocusable(false);// 不可以获得焦点

				}

				viewGroup.setVisibility(View.GONE);
			}
		});

		viewGroup.startAnimation(animation);
		
		
		
		
	}
	
	
	
	

}


那就ok啦,代码需要自己看看~~~~~






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值