Android--简单的三级菜单
关于这个菜单应该在很多播放器应用里面可以看见,直接先上两张效果图吧,一张是该Demo的效果图,一张是优酷手机客户端的效果图。

DEMO的效果图 优酷手机客户端界面
因为没有时间去自己制作图标,所以Demo里面采用的就是优酷手机客户端里的图标了。
一、布局
首先从效果图中可以看出来,该菜单界面分成了三层,所以,我们采用RelativeLayout布局方式来排列这三层菜单背景。
其次,在每一层菜单中,每一个图标也是相对于自己菜单级有固定位置的,所以每一个菜单级也采用RelativeLayout布局方式。
既然布局方式都分析清楚了,那么就可以直接开始写布局文件了。需要注意的就是,每一个图标之间相对位置的摆放,这样才能使整体效果更好。
<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"
tools:context=".MainActivity" > <RelativeLayout
android:id="@+id/level1"
android:layout_width="100dip"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level1" > <ImageView
android:id="@+id/icon_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_home"
android:layout_centerInParent="true"/>
</RelativeLayout> <RelativeLayout
android:id="@+id/level2"
android:layout_width="180dip"
android:layout_height="90dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level2" > <ImageView
android:id="@+id/icon_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_search"
android:layout_alignParentBottom="true"
android:layout_margin="10dip"/> <ImageView
android:id="@+id/icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_menu"
android:layout_marginTop="5dip"
android:layout_centerHorizontal="true"/> <ImageView
android:id="@+id/icon_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_margin="10dip"/> </RelativeLayout> <RelativeLayout
android:id="@+id/level3"
android:layout_width="280dip"
android:layout_height="140dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level3" > <ImageView
android:id="@+id/channel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/channel1"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dip"
android:layout_marginBottom="10dip"/> <ImageView
android:id="@+id/channel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/channel2"
android:layout_above="@id/channel1"
android:layout_alignLeft="@id/channel1"
android:layout_marginLeft="18dip"
android:layout_marginBottom="6dip"/> <ImageView
android:id="@+id/channel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/channel2"
android:layout_alignLeft="@id/channel2"
android:background="@drawable/channel3"
android:layout_marginLeft="30dip"
android:layout_marginBottom="6dip"/> <ImageView
android:id="@+id/channel4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/channel4"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"/> <ImageView
android:id="@+id/channel7"
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="10dip"
android:layout_marginBottom="10dip"/> <ImageView
android:id="@+id/channel6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/channel6"
android:layout_above="@id/channel7"
android:layout_alignRight="@id/channel7"
android:layout_marginRight="18dip"
android:layout_marginBottom="6dip"/> <ImageView
android:id="@+id/channel5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/channel6"
android:layout_alignRight="@id/channel6"
android:background="@drawable/channel5"
android:layout_marginRight="30dip"
android:layout_marginBottom="6dip"/> </RelativeLayout> </RelativeLayout>
二、MainActivity加载布局文件
在MainActivity.java中,通过onCreate()方法,来加载出布局文件。
三、实现动画效果和点击事件
首先分析可以得出,每一级菜单消失和出现的动画都是相似的,所以可以在此建立一个工具类,来统一编写动画效果的代码,在此建立MyUtils.java
整体的动画效果就分为,消失和出现,所以只用实现这两个方法就好了。
/**
* 出现的动画效果* @param view 需要动画效果的view* @startOffset 需要延时执行的时间
*/
public static void startAnimIn(RelativeLayout view, int startOffset) {
RotateAnimation animation = new RotateAnimation(180, 360, view.getWidth()/2, view.getHeight());
animation.setDuration(500); // 设置运行的时间
animation.setFillAfter(true); // 动画执行完以后保持最后的状态
animation.setStartOffset(startOffset); // 设置延时执行的时间
view.startAnimation(animation);
}在代码中,采用了RotateAnimation这个类,来控制view的动画效果。我们需要的是一个旋转的动画效果,所以我们需要知道默认圆心为 view的左上角,在此需要设置为 view的中心,即(view.getWidth()/2, view.getHeight())根据同样的道理,只需要修改一样动画效果的旋转角度,就可以写出消失动画的效果/**
* 让指定view 延时执行旋转离开的动画
* @param view
* @param i 延时时间
*/
public static void startAnimOut(RelativeLayout view, int startOffset) { RotateAnimation animation = new RotateAnimation(0, 180, view.getWidth()/2, view.getHeight());
animation.setDuration(500); // 设置运行的时间
animation.setFillAfter(true); // 动画执行完以后保持最后的状态
animation.setStartOffset(startOffset); // 设置延时执行的时间
view.startAnimation(animation);
}动画效果的类完成之后,就可以返回MainActivity中去监听按键的点击事件了。@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.icon_menu: // 处理menu图标的点击事件
// 如果第三级菜单是显示状态,则将其隐藏
if(isLevel3Show) {
// 隐藏
MyUtils.startAnimOut(level3,0);
} else {
// 显示
MyUtils.startAnimIn(level3,0);
} isLevel3Show = !isLevel3Show; break; case R.id.icon_home: // 处理home图标的点击事件
// 如果二级菜单是显示状态,那么就隐藏2,3级菜单
if(isLevel2Show ) {
MyUtils.startAnimOut(level2,0);
isLevel2Show = false;
if(isLevel3Show) {
MyUtils.startAnimOut(level3,200);
isLevel3Show = false;
}
} else {
// 如果二级菜单是隐藏状态,那么就显示2级菜单
MyUtils.startAnimIn(level2,0);
isLevel2Show = true;
}
break; default:
break;
}
}最后就是修改Menu按键的事件,使按下Menu可以隐藏或显示全部菜单的效果。
四、Demo下载
Android--简单的三级菜单的更多相关文章
- Python3学习之路~2.5 简单的三级菜单程序
程序:三级菜单 需求: 1.打印省.市.县三级菜单2.可返回上一级3.可随时退出程序 代码1: data={ "山东":{ "济南":["历下区&qu ...
- Python实现简单的三级菜单
话不多说,直奔代码 # 要处理的字典 dic1 = { '北京': { '东城': { '沙河': ['沙河机场', '链家'], '天通苑': ['北方明珠', '天通尾货'] }, '朝阳': { ...
- 三级菜单的实现(python程序)
这是刚开始写程序,三级菜单的程序基本是用字典实现,很low,以后学习了其他更好的东西,我会继续上传,然后争取在我水平高深之后,把这个简单的东西实现的狠高大上. _author_ = "zha ...
- android 三级菜单 BaseExpandableListAdapter
在网上搜了非常长时间.没有找到合适的Android三级菜单.所以就自己动手写了一个,主要使用了BaseExpandableList来实现,通过三个布局文件来完毕相应的菜单项,详细实现请參照下图. wa ...
- Python_简单三级菜单制作
一:制作要求 1.三级菜单 2.可依次选择进入各子菜单 3.所需新知识点:字典,列表 *本文通过三种方法完成,第一种:只使用循环,第二种:使用列表,第三种:使用字典 二:FlowChart流程图 与上 ...
- Android自己定义控件:老版优酷的三级菜单(效果图 + Demo)
效果图: 制作思路: 1.先分析这个效果,事实上能够理解为把三级菜单分成level1,level2,level3,level1是始终显示的. 点击level1后,level2会出现:点击level2后 ...
- Python学习-------------------三级菜单简单版
题目: 多级菜单 1.三级菜单 2.可依次选择进入的各子菜单 3.所需新知识点:列表.字典 ReadMe: 这个做循环,比较绕脑子 点开运行即可 Min ...
- HTML+CSS实现简单三级菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python(5)- 简单练习:python三级菜单优化
python三级菜单优化,菜鸟版链接:http://www.cnblogs.com/xuyaping/p/6648170.html menu = { '北京':{ '海淀':{ '五道口':{ 'so ...
- css三级菜单效果
一个简单实用的css三级菜单效果 <!doctype html> <html> <head> <meta charset="utf-8"& ...
随机推荐
- IOS开发--数据持久化篇文件存储(二)
前言:个人觉得开发人员最大的悲哀莫过于懂得使用却不明白其中的原理.在代码之前我觉得还是有必要简单阐述下相关的一些知识点. 因为文章或深或浅总有适合的人群.若有朋友发现了其中不正确的观点还望多多指出,不 ...
- Windows 下如何设置 只允许固定IP远程访问
通过设置IP安全策略限制固定IP访问 说明: (1)以XP环境为例,步骤:先禁止所有IP,再允许固定IP访问. (2)配置过程中很多步骤图是重复的,一些没价值的图就省略了: (3)光看的话可能中间重复 ...
- 《解剖PetShop》系列转载
1 <解剖PetShop>系列之六 PetShop之表示层设计 http://ityup.com/showtopic-8.html 2 <解剖PetShop>系列之五 ...
- 使用jdk操作 wsdl2java (wedservice)
打开jdk下的bin目录 看下能否找到"wsimport.exe"这个文件 一般情况下都会有 如果没有则说明你的JDK不支持这个功能 然后在DOS窗口下输入wsimport 敲回车 ...
- 寻ta分析与站点内容
从 寻ta 突然来的訪问量就開始在想.站点内容是否才是真正须要的东西. 寻ta分析 作为一篇文章带来的影响,我们能够看看訪问会话. 日期 訪问量 5.5 9 5.6 4618 5.7 1216 5.8 ...
- [OSG]如何用Shader得到物体的世界坐标
来自:http://www.cnblogs.com/hesicong/archive/2008/05/27/1208312.html 最近群里面有个朋友问我关于如何得到OpenGL世界坐标的问题,当时 ...
- 进程和cpu的相关知识和简单调优方案
进程就是一段执行的程序,每当一个程序执行时.对于操作系统本身来说,就创建了一个进程,而且分配了相应的资源.进程能够分为3个类别: 1.交互式进程(I/O) 2.批处理进程 (CPU) ...
- 动态SQL使用小结
1.什么是动态SQL? 静态 SQL:静态 SQL 语句一般用于嵌入式 SQL 应用中,在程序运行前,SQL 语句必须是确定的,例如 SQL 语句中涉及的列名和表名必须是存在的.静态 SQL 语句的编 ...
- [React] Styling React Components With Aphrodite
Aphrodite is a library styling React components. You get all the benefits of inline styles (encapsul ...
- ios开发——实用技术篇Swift篇&加速计和陀螺仪
加速计和陀螺仪 //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnim ...