Android -- MeasureSpec
自定义控件都会去重写View的onMeasure方法,因为该方法指定该控件在屏幕上的大小。
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
onMeasure传入的两个参数是由上一层控件传入的大小,有多种情况,重写该方法时需要对计算控件的实际大小,然后调用setMeasuredDimension(int, int)设置实际大小。
onMeasure传入的widthMeasureSpec和heightMeasureSpec不是一般的尺寸数值,而是将模式和尺寸组合在一起的数值。
常用的三个函数
int getMode(int measureSpec)//根据提供的测量值(格式)提取模式(上述三个模式之一)
int getSize(int measureSpec)//根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小)
int makeMeasureSpec(int size,int mode)//根据提供的大小值和模式创建一个测量值(格式)
得到模式:
int mode = MeasureSpec.getMode(widthMeasureSpec)
得到尺寸:
int size = MeasureSpec.getSize(widthMeasureSpec)
Mode
mode共有三种情况,取值分别为
MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST
- MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。
- MeasureSpec.AT_MOST是最大尺寸,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。
- MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。
Code
控件根据横竖屏、屏幕大小自适应:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
} public void fitWindow(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;//屏幕宽
mRatioHeight = height;//屏幕高
requestLayout();//促使调用onMeasure
}
我是天王盖地虎的分割线
Android -- MeasureSpec的更多相关文章
- android MeasureSpec的三个测量模式
1.MeasureSpec含义 其实可以去看MeasureSpec的文档,里面对MeasureSpec的作用介绍得很清楚.MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureS ...
- Android——MeasureSpec学习 - 解决ScrollView嵌套ListView和GridView冲突的方法
原文地址:http://blog.csdn.net/yuhailong626/article/details/20639217 在自定义View和ViewGroup的时候,我们经常会遇到int ...
- Android -- onMeasure
onMeasure调用次数 当Activity获取焦点的时候,它就需要绘制布局.Android框架会处理绘制过程,但这个Activity必须提供它布局树的根节点. 绘制过程是从布局的根节点开始的.这个 ...
- Android的onMeasure和onLayout And MeasureSpec揭秘
Android中自定义ViewGroup最重要的就是onMeasure和onLayout方法,都需要重写这两个方法,ViewGroup绘制 的过程是这样的:onMeasure → onLayout → ...
- android学习——MeasureSpec介绍及使用
一.MeasureSpc类说明 SDK的介绍:MeasureSpc类封装了父View传递给子View的布局(layout)要求.每个MeasureSpc实例代表宽度或者高度 它有三种模式:①.UNSP ...
- Android开发之View重写相关API-onLayout,onMeasure,MeasureSpec
1.onLayout android.view.ViewGroup protected void onLayout(boolean changed, int l, int t, int r, int ...
- Android中自定义View的MeasureSpec使用
有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/guide/topics/ui/custo ...
- ANDROID自定义视图——onMeasure流程,MeasureSpec详解
简介: 在自定义view的时候,其实很简单,只需要知道3步骤: 1.测量——onMeasure():决定View的大小 2.布局——onLayout():决定View在ViewGroup中的位置 3. ...
- ANDROID定义自己的看法——onMeasure,MeasureSpec源代码 过程 思考具体解释
一个简短的引论: 在他们的定义view什么时候,其实很easy,只需要知道3: 1.測量--onMeasure():决定View的大小 2.布局--onLayout():决定View在ViewGrou ...
随机推荐
- python opencv3 滤波器 卷积核
git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 import numpy as np from sc ...
- 高并发编程之synchronized
一.什么是线程? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程 ...
- BZOJ 3572: [Hnoi2014]世界树 虚树 树形dp
https://www.lydsy.com/JudgeOnline/problem.php?id=3572 http://hzwer.com/6804.html 写的时候参考了hzwer的代码,不会写 ...
- ThinkPHP快速实现数据分页(前端/后端分离)
数据 分页 可能是web 编程里最常用到的功能之一.thinkphp 实现分页功能十分简洁.只需要定义 几个参数 就能搞定.当然,扩展也是十分方便的. 让我们现在就开始thinkphp的分页实现吧. ...
- SQL Server中执行正则表达式
总体方案:写function,再执行update语句. 一.查询函数 -- ============================================= -- Author: <l ...
- Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题
A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...
- POJ 1904 King's Quest tarjan
King's Quest 题目连接: http://poj.org/problem?id=1904 Description Once upon a time there lived a king an ...
- UVALive 4868 Palindrometer 暴力
F - Palindrometer Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- 邮件基本常识普及(to/cc/bcc)
http://blog.sina.com.cn/s/blog_5572b4b5010009ul.html 前两天,某个同事发的一封邮件着实把我给郁闷了,他发的是图片形式的笑话,内容稍稍有点不太健康,这 ...
- Red Hat Enterprise Linux 7.4配置VSFTP服务器
vsftpd(very secure ftp daemon,非常安全的FTP守护进程)是一款运行在Linux操作系统上的FTP服务程序,不仅完全开源而且免费,此外,还具有很高的安全性.传输速度,以及支 ...