自定义控件之仿优酷菜单
来源:互联网 发布:阿里云域名优惠码 编辑:程序博客网 时间:2024/04/30 07:27
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);}@Overridepublic void onClick(View v) {//getId : 获取被点击的按钮的idswitch (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,0setAnimation(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() {//动画开始调用的方法@Overridepublic void onAnimationStart(Animation animation) {isStart++;}//动画重复执行调用的方法@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}//动画结束调用的方法@Overridepublic 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 : 判断控件是否是ViewGroupif (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
- 自定义控件之仿优酷菜单
- Android自定义控件之仿优酷菜单
- android 自定义控件之圆形菜单
- 自定义控件之优酷菜单
- 自定义控件之侧拉菜单SlidingMenu
- 自定义控件之侧滑菜单
- Android自定义控件之钟摆菜单
- 自定义控件菜单
- android控件-自定义菜单
- android 自定义菜单控件
- 自定义控件 防优酷菜单
- 自定义控件:旋转菜单
- Android开发之自定义控件-YouKu圆盘菜单
- 自定义控件专题之三:侧滑菜单
- android自定义控件之模仿优酷菜单
- 自定义控件学习之:追溯以前的优酷菜单
- 自定义控件:侧滑菜单
- Winform 自定义图片菜单控件
- poj 3694 Network
- HDU#1040:As Easy As A+B
- 一个DIV做的LODING动画CSS3动画
- 剑指offer:数组中只出现一次的数字(java)
- 【深度学习:目标检测】RCNN学习笔记(5):faster rcnn
- 自定义控件之仿优酷菜单
- TCP拥塞控制算法内核实现剖析
- 洛谷 P2679 子串
- 消除运放的自激
- 【深度学习:目标检测】RCNN学习笔记(6):You Only Look Once(YOLO):Unified, Real-Time Object Detection
- iOS6、7、8、9新特性汇总和适配说明 ++
- 我们在继承父类要实现相应功能如果不需要父类的某个功能可以置空或执行方法里啥也不填
- 【深度学习:目标检测】RCNN学习笔记(7):Faster R-CNN 英文论文翻译笔记
- eclipse python中文乱码