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

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

关于WebSocket协议状态码解析

《关于WebSocket协议状态码解析》:本文主要介绍关于WebSocket协议状态码的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录WebSocket协议状态码解析1. 引言2. WebSocket协议状态码概述3. WebSocket协议状态码详解3

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI