自定义控件之onMeasure解析01

2023-10-20 04:32

本文主要是介绍自定义控件之onMeasure解析01,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

好吧,为何要写这个系列的文章呢,因为最近看了一些源码,很多源控件都是自定义控件,因此有必要重新对自定义控件进行系统的学习。

知识点:自定义控件一般继承View,也可直接继承已有的控件。不管哪种情况,核心思想还是按照:onMeasure->onLayout->onDraw这个步骤来。

1.onMeasure()

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="300dp"android:layout_height="400dp"><com.autoviewpager.widget.IndicatorViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></LinearLayout>
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int withMode = MeasureSpec.getMode(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int withSize = MeasureSpec.getSize(widthMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);L.e("onMeasure-->withMeasureSpec:"+widthMeasureSpec+"  ---withMode:"+withMode+"  ---withSize:"+withSize);}

当自定义控件设置了具体的宽高时,我们看输出结果:

onMeasure-->withMeasureSpec:-2147482748  ---withMode:-2147483648  ---withSize:900

发现:withMeayousureSpec = withMode + withSize.可以看到:
withMode = 2^31,并且withMeasureSpec为int型,占32位,所以withMode应该是withMeasureSpec的高位,而withSize为其低位上的数值。再通过MeasureSepc源码得知它是通过装载和卸载mode、size元组于int数中来简化对象的分配。也就是说MeasureSpec是由高两位size+低30位mode组成的32位int数来表示。
withMode有三种:

private static final int MODE_SHIFT = 30;
/*** Measure specification mode: The parent has not imposed any constraint* on the child. It can be whatever size it wants.*/public static final int UNSPECIFIED = 0 << MODE_SHIFT;/*** Measure specification mode: The parent has determined an exact size* for the child. The child is going to be given those bounds regardless* of how big it wants to be.*/public static final int EXACTLY     = 1 << MODE_SHIFT;/*** Measure specification mode: The child can be as large as it wants up* to the specified size.*/public static final int AT_MOST     = 2 << MODE_SHIFT;

可以看出,三种mode分别为00、01、10左移30位得到,因此,上面打印得到的mode=-2147483648对应这里的ModeSpec.AT_MOST;而size则对应的是自定义控件父布局的宽。
然后,我们将布局文件修改一下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="300dp"android:layout_height="400dp"><com.autoviewpager.widget.IndicatorViewandroid:layout_width="200dp"android:layout_height="wrap_content" /></LinearLayout></LinearLayout>

输出结果为:onMeasure-->withMeasureSpec:1073742424 ---withMode:1073741824 ---withSize:600
这时,withMode = 2^30 = SpecMode.EXACTLY;withsize对应其本身的宽。
接下来,我们将布局文件再改一下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><com.autoviewpager.widget.IndicatorViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></LinearLayout>

打印结果:onMeasure-->withMeasureSpec:-2147482568 ---withMode:-2147483648 ---withSize:1080
这时:mode = 2^31 = MeasureSpec.AT_MOST ;size则为手机宽的尺寸。
其他更改布局就补贴出来了,通过更改布局打印结果,我们基本可以得出以下结论:
这里我们假定自定义控件为customView,其父窗体为PcustomView.那么:

  1. 当PcustomView.Mode = MeasureSpec.EXACTLY时:
    a.如果customView设置with 为具体的值,那么customView.Mode = MeasureSpec.EXACTLY, customView.size = 它设置的具体值;
    b.如果customView设置with为wrap_content,那么customView.Mode = MeasureSpec.AT_MOST,customView.size =父窗体所提供的大小;
    c.如果customView设置为match_parent,那么customView.Mode = MeasureSpec.EXACTLY,customView.size = 父窗体提供的大小

  2. 当PcustomView.Mode = MeasureSpec.AT_MOST时:
    a.如果customView设置with 为具体的值,那么customView.Mode = MeasureSpec.EXACTLY, customView.size = 它设置的具体值;
    b.如果customView设置with为wrap_content,那么customView.Mode = MeasureSpec.AT_MOST,customView.size =父窗体所提供的大小;
    c.如果customView设置为match_parent,那么customView.Mode = MeasureSpec.EXACTLY,customView.size = 父窗体提供的大小.

  3. 当PcustomView.Mode = MeasureSpec. UNSPECIFIED时:
    a.如果customView设置with 为具体的值,那么customView.Mode = MeasureSpec.EXACTLY, customView.size = 它设置的具体值;
    b.如果customView设置with为wrap_content,那么customView.Mode = MeasureSpec.UNSPECIFIED,customView.size =0;
    c.如果customView设置为match_parent,那么customView.Mode = MeasureSpec.UNSPECIFIED,customView.size = 0.

有了以上结论,我们就知道如何在onMeasure方法中设置自己想要的尺寸了,记得确定尺寸后要调用` setMeasuredDimension(int measuredWidth, int measuredHeight)方法。这个方法是将确定的长宽值设置到画布中。

这篇关于自定义控件之onMeasure解析01的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

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

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor