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

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

OWASP十大安全漏洞解析

OWASP(开放式Web应用程序安全项目)发布的“十大安全漏洞”列表是Web应用程序安全领域的权威指南,它总结了Web应用程序中最常见、最危险的安全隐患。以下是对OWASP十大安全漏洞的详细解析: 1. 注入漏洞(Injection) 描述:攻击者通过在应用程序的输入数据中插入恶意代码,从而控制应用程序的行为。常见的注入类型包括SQL注入、OS命令注入、LDAP注入等。 影响:可能导致数据泄

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完

一、题目阅读 (最大值之和)给定整数序列 a0,⋯,an−1,求该序列所有非空连续子序列的最大值之和。上述参数满足 1≤n≤105 和 1≤ai≤108。 一个序列的非空连续子序列可以用两个下标 ll 和 rr(其中0≤l≤r<n0≤l≤r<n)表示,对应的序列为 al,al+1,⋯,ar​。两个非空连续子序列不同,当且仅当下标不同。 例如,当原序列为 [1,2,1,2] 时,要计算子序列 [

多线程解析报表

假如有这样一个需求,当我们需要解析一个Excel里多个sheet的数据时,可以考虑使用多线程,每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要提示解析完成。 Way1 join import java.time.LocalTime;public class Main {public static void main(String[] args) thro