自定义菜单,仿优酷菜单
效果图如下:点击主页后,上面2个圆环旋转消失与出现,点击中间圆环的中间那个菜单按钮,最外围的圆环旋转消失于出现
利用了自定义控件技术,以及图片的旋转和布局时各个控件的相对关系。
1、acitivity_main.xml的布局文件
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.yuanlp.youkudemo.MainActivity"><RelativeLayoutandroid:id="@+id/level3"android:layout_width="280dp"android:layout_height="140dp"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:background="@drawable/level3"><ImageViewandroid:id="@+id/channel1"android:layout_marginLeft="8dp"android:layout_marginBottom="8dp"android:layout_alignParentBottom="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/channel1"/><ImageViewandroid:id="@+id/channel2"android:layout_above="@+id/channel1"android:layout_marginLeft="34dp"android:layout_marginBottom="8dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/channel2"/><ImageViewandroid:id="@+id/channel3"android:layout_above="@+id/channel2"android:layout_marginLeft="66dp"android:layout_marginBottom="8dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/channel3"/><ImageViewandroid:id="@+id/channel4"android:layout_marginTop="8dp"android:layout_centerHorizontal="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/channel4"/><ImageViewandroid:id="@+id/channel5"android:layout_above="@+id/channel6"android:layout_marginRight="67dp"android:layout_marginBottom="8dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:src="@drawable/channel5"/><ImageViewandroid:id="@+id/channel6"android:layout_above="@+id/channel7"android:layout_marginRight="27dp"android:layout_marginBottom="8dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:src="@drawable/channel6"/><ImageViewandroid:id="@+id/channel7"android:layout_marginRight="9dp"android:layout_marginBottom="8dp"android:layout_alignParentBottom="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:src="@drawable/channel7"/></RelativeLayout><RelativeLayoutandroid:id="@+id/level2"android:layout_width="180dp"android:layout_height="90dp"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:background="@drawable/level2"><ImageViewandroid:layout_marginLeft="8dp"android:layout_marginBottom="8dp"android:layout_alignParentBottom="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/icon_search"/><ImageViewandroid:id="@+id/icon_menu"android:layout_centerHorizontal="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:src="@drawable/icon_menu"/><ImageViewandroid:layout_marginLeft="8dp"android:layout_marginBottom="8dp"android:layout_alignParentBottom="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:src="@drawable/icon_myyouku"/></RelativeLayout><RelativeLayoutandroid:id="@+id/level1"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true"android:layout_width="100dp"android:layout_height="50dp"android:background="@drawable/level1"><ImageViewandroid:id="@+id/icon_home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/icon_home"/></RelativeLayout></RelativeLayout>
2、Tools.java 工具类,具体实现了消失与出现的方法,利用了图片的旋转消失
packagecom.yuanlp.youkudemo;importandroid.view.View;importandroid.view.animation.RotateAnimation;/***Createdby原立鹏on2017/7/3.*控制level的指定的看控件*/classTools{publicstaticvoidhideView(Viewview){//参数解释:第一个蚕食是从多少开始,第二个是旋转多少度,第三个以及第四个是相对于控件来说,旋转中心的位置,即控件宽度的一半以及高度RotateAnimationra=newRotateAnimation(0,180,view.getWidth()/2,view.getHeight());ra.setDuration(500);//设置动画的时间,不然的话直接就没了,没有视觉效果ra.setFillAfter(true);//动画停留在完成的状态view.startAnimation(ra);//启动动画}publicstaticvoidshowView(Viewview){//参数解释:第一个蚕食是从多少开始,第二个是旋转多少度,第三个以及第四个是相对于控件来说,旋转中心的位置,即控件宽度的一半以及高度RotateAnimationra=newRotateAnimation(180,360,view.getWidth()/2,view.getHeight());ra.setDuration(500);//设置动画的时间,不然的话直接就没了,没有视觉效果ra.setFillAfter(true);//动画停留在完成的状态view.startAnimation(ra);//启动动画}}
3、MainActivity.java
packagecom.yuanlp.youkudemo;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.view.View;importandroid.widget.ImageView;importandroid.widget.RelativeLayout;publicclassMainActivityextendsAppCompatActivity{privateImageViewicon_home;privateImageViewicon_menu;privateRelativeLayoutlevel1;privateRelativeLayoutlevel2;privateRelativeLayoutlevel3;//定义一个状态,来控制level3是否显示privatebooleanisShowLevel3=true;//定义一个状态,来控制level2是否显示privatebooleanisShowLevel2=true;//定义一个状态,来控制level1是否显示privatebooleanisShowLevel1=true;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);level1=(RelativeLayout)findViewById(R.id.level1);level2=(RelativeLayout)findViewById(R.id.level2);level3=(RelativeLayout)findViewById(R.id.level3);icon_home=(ImageView)findViewById(R.id.icon_home);icon_menu=(ImageView)findViewById(R.id.icon_menu);MyOnClickListenermyOnClickListener=newMyOnClickListener();icon_home.setOnClickListener(myOnClickListener);icon_menu.setOnClickListener(myOnClickListener);}classMyOnClickListenerimplementsView.OnClickListener{@OverridepublicvoidonClick(Viewv){switch(v.getId()){caseR.id.icon_home://如果三级菜单和二级菜单都是现实的,那么把2个都设置隐藏if(isShowLevel2){isShowLevel2=false;Tools.hideView(level2);if(isShowLevel3){isShowLevel3=false;Tools.hideView(level3);}}else{isShowLevel2=true;Tools.showView(level2);}//如果三级菜单和二级菜单都是隐藏的,就显示二级菜单break;caseR.id.icon_menu:if(isShowLevel3){//隐藏isShowLevel3=false;Tools.hideView(level3);}else{isShowLevel3=true;Tools.showView(level3);}break;}}}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。