自定义控件之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

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s