Android实现卫星菜单(一)自定义属性及重写onMeasure、onLayout方法

本文主要是介绍Android实现卫星菜单(一)自定义属性及重写onMeasure、onLayout方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

自定义ViewGroup

1.自定义属性

a、attr.xml

b、在布局文件中使用

c、在自定义控件中读取


res的values下新建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="ArcMenu"><attr name="position"/><attr name="radius"/></declare-styleable></resources>


ArcMenu.java

public class ArcMenu extends ViewGroup {private static final int POS_LEFT_TOP = 0;private static final int POS_LEFT_BOTTOM = 1;private static final int POS_RIGHT_TOP = 2;private static final int POS_RIGHT_BOTTOM = 3;private Position mPosition = Position.RIGHT_BOTTOM;private int mRadius;// 菜单的状态 打开或关闭private Status mCurrentStatus = Status.CLOSE;// 菜单的主按钮private View mCButton;private OnMenuItemClickListener mMenuItemClickListener;public enum Status {OPEN, CLOSE}/*** 菜单的位置枚举类*/public enum Position {LEFT_TOP, LEFT_BOTTOM, RIGHT_TOP, RIGHT_BOTTOM}/*** 点击子菜单项的回调接口*/public interface OnMenuItemClickListener {void onClick(View view, int pos);}public void setOnMenuItemClickListener(OnMenuItemClickListener mMenuItemClickListener) {this.mMenuItemClickListener = mMenuItemClickListener;}public ArcMenu(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ArcMenu(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100,getResources().getDisplayMetrics());// 获取自定义属性TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ArcMenu,defStyleAttr, 0);int pos = a.getInt(R.styleable.ArcMenu_position, POS_RIGHT_BOTTOM);switch (pos) {case POS_LEFT_TOP:mPosition = Position.LEFT_TOP;break;case POS_LEFT_BOTTOM:mPosition = Position.LEFT_BOTTOM;break;case POS_RIGHT_TOP:mPosition = Position.RIGHT_TOP;break;case POS_RIGHT_BOTTOM:mPosition = Position.RIGHT_BOTTOM;break;}mRadius = (int) a.getDimension(R.styleable.ArcMenu_radius,TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100,getResources().getDisplayMetrics()));Log.i("TAG", "position = " + mPosition + ", radius = " + mRadius);a.recycle();}public ArcMenu(Context context) {this(context, null);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {}}

activity_main

<?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:lin="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><com.example.arcmenu.view.ArcMenuandroid:layout_width="match_parent"android:layout_height="match_parent"lin:position="right_bottom"lin:radius="100dp"></com.example.arcmenu.view.ArcMenu><com.example.arcmenu.view.ArcMenuandroid:layout_width="match_parent"android:layout_height="match_parent"lin:position="left_top"lin:radius="130dp"></com.example.arcmenu.view.ArcMenu></RelativeLayout>




转化成px输出了


https://github.com/daCapricorn/ArcMenu/tree/master/sample/res/drawable 这里有一些图片素材


主按钮由两张图片拼成


activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:lin="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><com.example.arcmenu.view.ArcMenuandroid:layout_width="match_parent"android:layout_height="match_parent"lin:position="right_bottom"lin:radius="100dp"><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/composer_button"><ImageViewandroid:id="@+id/id_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/composer_icn_plus"/></RelativeLayout><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/composer_music"android:tag="Music"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/composer_place"android:tag="Place"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/composer_sleep"android:tag="Sleep"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/composer_thought"android:tag="Sun"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/composer_with"android:tag="People"/></com.example.arcmenu.view.ArcMenu></RelativeLayout>



2.onMeasure

3.onLayout


MainActivity中添加

    @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int count = getChildCount();for (int i = 0; i < count; i++) {// 测量childmeasureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {if (changed) {layoutCButton();}}/*** 定位主菜单按钮*/private void layoutCButton() {mCButton = getChildAt(0);mCButton.setOnClickListener(this);int l = 0;int t = 0;int width = mCButton.getMeasuredWidth();int height = mCButton.getMeasuredHeight();switch (mPosition) {case LEFT_TOP:l = 0;t = 0;break;case LEFT_BOTTOM:l = 0;// 纵轴 控件的高度减去按钮的高度,让其显示在最下面t = getMeasuredHeight() - height;break;case RIGHT_TOP:l = getMeasuredWidth() - width;t = 0;break;case RIGHT_BOTTOM:l = getMeasuredWidth() - width;t = getMeasuredHeight() - height;break;}// 定位mCButton.layout(l, t, l + width, t + width);}


效果





这篇关于Android实现卫星菜单(一)自定义属性及重写onMeasure、onLayout方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/510414

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll