Android卫星菜单效果的实现方法
Android小白第一次写博客,心情无比激动。下面给大家展示一下卫星菜单的实现。
1.简单介绍卫星菜单
在应用程序中,有很多展示菜单的方式,但其功能都是大同小异,这样一来,菜单的美观以及展示方式就显的尤为重要,卫星菜单就是很不错的一种。下面是本案例的gif图:
2.学习本案例需要的知识点
(1)动画
(2)自定义ViewGroup
(3)自定义属性
a、attr.xml
b、在布局中使用自定义属性
c、在代码中获取自定义属性值
3.首先分析我们的卫星菜单需要那些自定义属性并书写代码
首先,菜单可以显示在屏幕的四个角,所以我们需要一个属性来确定它的位置,菜单在屏幕的四个角比较美观,在这里用到枚举。
其次,我们还需要一个展开半径,因此还需要自定义半径。
下面是attr.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="position"> <enum name="left_top" value="0" /> <enum name="left_bottom" value="1" /> <enum name="right_top" value="2" /> <enum name="right_bottom" value="3" /> </attr> <attr name="radius" format="dimension"/> <declare-styleable name="SateMenu"> <attr name="radius" /> <attr name="position" /> </declare-styleable> </resources>
4.自定义ViewGroup
?继承ViewGroup 以相关属性
public class SateMenu extends ViewGroup implements View.OnClickListener { private int animationTime; //动画时间 private int radius; //展开半径 private int pos; //从自定义属性中获取的菜单位置 private State state; //菜单状态 private int l = 0, t = 0; //左上值 private View centerBtn = null; //展开按钮 private MenuItemListener menuItemListener; //菜单项点击监听 private Position position; //枚举型菜单位置 private enum Position { //位置枚举 LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM } private enum State { //菜单状态枚举 OPEN, COLSE }
?构造方法
public SateMenu(Context context) { //一个参数构造方法调用两个参数构造方法 this(context, null); } public SateMenu(Context context, AttributeSet attrs) { //两个参数构造方法调用三个个参数构造方法 this(context, attrs, 0); } public SateMenu(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); animationTime = 500; //设置动画展开时间 TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SateMenu, defStyleAttr, 0); //获取自定义属性值集合 radius = (int) a.getDimension(R.styleable.SateMenu_radius, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics())); //获取半径并转化为像素值 state = State.COLSE; //设置菜单默认关闭 pos = a.getInt(R.styleable.SateMenu_position, 0); //获取位置 //将位置转化为枚举值 (这样就把无意义的int转化为有意义的枚举值) switch (pos) { case 0: position = Position.LEFT_TOP; break; case 1: position = Position.LEFT_BOTTOM; break; case 2: position = Position.RIGHT_TOP; break; case 3: position = Position.RIGHT_BOTTOM; break; } }
?重写onMeasure方法
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int count = getChildCount(); //测量子view for (int i = 0; i < count; i++) { measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec); } }
?重写onLayout方法
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (changed) btnLayout(); } private void btnLayout() { centerBtn = getChildAt(0); if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP) { //如果菜单设置在屏幕的右侧,那么展开按钮的l值=ViewGroup宽度-按钮宽度 l = getMeasuredWidth() - centerBtn.getMeasuredWidth(); } if (position == Position.LEFT_BOTTOM || position == Position.RIGHT_BOTTOM) { //如果菜单设置在屏幕的下边,那么展开按钮的t值=ViewGroup高度-按钮高度 t = getMeasuredHeight() - centerBtn.getMeasuredHeight(); } //设置展开按钮位置 centerBtn.layout(l, t, l + centerBtn.getMeasuredWidth(), t + centerBtn.getMeasuredHeight()); childBtnlayout(); //设置子按钮位置 centerBtn.setOnClickListener(this); }
?设置子按钮位置需要一点点数学知识,下面我以主菜单在右下角为例,画一个简图,图片对应右侧第一个公式
private void childBtnlayout() { int childMuneCount = getChildCount() - 1; //角度等于90度/子按钮个数-1 float a = (float) (Math.PI / 2 / (childMuneCount - 1)); int cl, ct; //分别是子按钮的 左 上 for (int i = 0; i < childMuneCount; i++) { if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP) { cl = (int) (l - radius * Math.cos(i * a)); } else { cl = (int) (l + radius * Math.cos(i * a)); } if (position == Position.LEFT_TOP || position == Position.RIGHT_TOP) { ct = (int) (t + radius * Math.sin(i * a)); } else { ct = (int) (t - radius * Math.sin(i * a)); } View childView = getChildAt(i + 1); childView.layout(cl, ct, cl + childView.getMeasuredWidth(), ct + childView.getMeasuredHeight()); childView.setOnClickListener(this); childView.setTag(i); childView.setVisibility(View.GONE); } }
?动画的展开与关闭,这里没有用属性动画,原理是:当用户关闭菜单的时候,将子按钮隐藏,打开才打的时候在把子按钮显示出来
private void changeState() { int childMuneCount = getChildCount() - 1; //设置展开按钮旋转动画 Animation animation = new RotateAnimation(0, 360, centerBtn.getMeasuredWidth() / 2, centerBtn.getMeasuredHeight() / 2); animation.setDuration(animationTime); centerBtn.setAnimation(animation); animation.start(); View childView; //子按钮有两个动画(位移、旋转),所以这里用到动画集,这里也涉及到一些数学知识,和之前设置子按钮位置差不多 AnimationSet animationSet; Animation translateAnimation; Animation rotateAnimation; int cl, ct; float a = (float) (Math.PI / 2 / (childMuneCount - 1)); if (state == State.OPEN) { state = State.COLSE; for (int i = 0; i < childMuneCount; i++) { if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP) cl = (int) (radius * Math.cos(i * a)); else cl = (int) (-radius * Math.cos(i * a)); if (position == Position.LEFT_TOP || position == Position.RIGHT_TOP) ct = (int) (-radius * Math.sin(i * a)); else ct = (int) (radius * Math.sin(i * a)); childView = getChildAt(i + 1); childView.setVisibility(View.GONE); translateAnimation = new TranslateAnimation(0, cl, 0, ct); translateAnimation.setDuration(animationTime); rotateAnimation = new RotateAnimation(0, 360, childView.getMeasuredHeight() / 2, childView.getMeasuredHeight() / 2); rotateAnimation.setDuration(animationTime); animationSet = new AnimationSet(true); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(translateAnimation); childView.setAnimation(animationSet); animationSet.start(); childView.setVisibility(View.GONE); } } else { state = State.OPEN; for (int i = 0; i < childMuneCount; i++) { if (position == Position.RIGHT_BOTTOM || position == Position.RIGHT_TOP) cl = (int) (radius * Math.cos(i * a)); else cl = (int) (-radius * Math.cos(i * a)); if (position == Position.LEFT_TOP || position == Position.RIGHT_TOP) ct = (int) (-radius * Math.sin(i * a)); else ct = (int) (radius * Math.sin(i * a)); childView = getChildAt(i + 1); childView.setVisibility(View.GONE); translateAnimation = new TranslateAnimation(cl, 0, ct, 0); translateAnimation.setDuration(animationTime); rotateAnimation = new RotateAnimation(360, 0, childView.getMeasuredHeight() / 2, childView.getMeasuredHeight() / 2); rotateAnimation.setDuration(animationTime); animationSet = new AnimationSet(true); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(translateAnimation); childView.setAnimation(animationSet); animationSet.start(); childView.setVisibility(View.VISIBLE); } } }
?写到这里我们的卫星菜单已经可以展现出来的,运行一下,效果还是不错的。美中不足的是,子按钮还没有点击事件,下面我们就将这个小小的不足补充一下。我们可以通过给子按钮添加点击事件来监听它,但点击之后要做的事情不可能写在ViewGroup中,这就需要用接口进行回调。大家看一下在设置子按钮位置的时候有这样一句代码 childView.setTag(i); 它的目的就是给子按钮添加索引,接下来看一下具体怎样实现的。
@Override public void onClick(View v) { if (v.getId() == centerBtn.getId()) { changeState(); } else { if (menuItemListener != null) { menuItemListener.onclick((Integer) v.getTag()); } } } public interface MenuItemListener { void onclick(int position); } public void setMenuItemListener(MenuItemListener menuItemListener) { this.menuItemListener = menuItemListener; }
?到这里我们已经完全实现了卫星菜单的所有功能,但大家有没有发现,一些菜单在展开之后,我们点击其他区域,菜单会自动收起来,所以我们还要给我们的ViewGroup添加onTouchEvent事件,在菜单展开的时候,他把菜单收起来,并将此次点击拦截。
@Override public boolean onTouchEvent(MotionEvent event) { if (state == State.OPEN) { changeState(); return true; //拦截 } return super.onTouchEvent(event); }
5.下面试用一下我们编写的卫星菜单,看一下成果。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:wzw="http://schemas.android.com/apk/res/com.satemenudemo" android:layout_width="match_parent" android:layout_height="match_parent"> <com.satemenudemo.SateMenu android:id="@+id/menu_id" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="3dp" wzw:position="right_bottom" wzw:radius="150dp"> <ImageButton android:id="@+id/center_btn" android:layout_width="40dp" android:layout_height="40dp" android:background="@drawable/add" /> <ImageButton android:id="@+id/menu1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/find" /> <ImageButton android:id="@+id/menu2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shop" /> <ImageButton android:id="@+id/menu3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/people" /> <ImageButton android:id="@+id/menu4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/love" /> </com.satemenudemo.SateMenu> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SateMenu sateMenu = (SateMenu) findViewById(R.id.menu_id); sateMenu.setMenuItemListener(new SateMenu.MenuItemListener() { @Override public void onclick(int position) { Toast.makeText(MainActivity.this, "-- "+position, Toast.LENGTH_SHORT).show(); } }); } }
以上所述是小编给大家介绍的Android卫星菜单效果的实现方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
- 什么是编译错误,运行时错误及逻辑错误?
- 我的WCF之旅(4):WCF中的序列化[上篇]
- WCF技术剖析之二十八:自己动手获取元数据[附源代码下载]
- 微信年度重磅“小游戏”上线,罗胖一度退出的小程序正在逆袭
- 谈谈WCF中的Data Contract(2):WCF Data Contract对Generic的支持
- Android注解学习(1)
- [WCF权限控制]ASP.NET Roles授权[上篇]
- [WCF权限控制]ASP.NET Roles授权[下篇]
- 如何解决分布式系统中的跨时区问题[实例篇]
- Visual Studio对程序集签名时一个很不好用的地方
- 一个关于解决序列化问题的编程技巧
- [WCF权限控制]从两个重要的概念谈起:Identity与Principal[上篇]
- 从数据到代码——通过代码生成机制实现强类型编程[下篇]
- 谈谈你最熟悉的System.DateTime[下篇]
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- AndroidStudio项目打包成jar的简单方法
- 浅谈React Native打包apk的坑
- Android 设置主题实现点击波纹效果的示例
- 更新Android Studio 3.0碰到的问题小结
- android实现一个图片验证码倒计时功能
- Android添加glide库报错Error:Failed to resolve:com.android.support:26.0.2的解决
- Android多线程下载示例详解
- 详解Android Gradle插件3.0挖坑日记
- Android开发之拼音转换工具类PinyinUtils示例
- Android多线程断点续传下载示例详解
- Android设备与外接U盘实现数据读取操作的示例
- [Alibaba-ARouter]浅谈简单好用的Android页面路由框架
- android屏幕圆角实现方法的示例代码
- Android开发中日期工具类DateUtil完整实例
- Android模仿实现微博详情页滑动固定顶部栏的效果实例