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

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

MySQL 打开binlog日志的方法及注意事项

《MySQL打开binlog日志的方法及注意事项》本文给大家介绍MySQL打开binlog日志的方法及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、默认状态二、如何检查 binlog 状态三、如何开启 binlog3.1 临时开启(重启后失效)

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使