onMeasure实例分析
2024-08-25 11:12:46
本文转自:http://blog.csdn.net/u012604322/article/details/17097105
上面这个两个视图是Android API中没有给出来的但在来电接听和闹钟被使用到的一个widget视图——GlowPadView.java
我们通过源码来看看这个View的大小是怎么通过onMeasure来控制的。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int minimumWidth = getSuggestedMinimumWidth();
final int minimumHeight = getSuggestedMinimumHeight();
int computedWidth = resolveMeasured(widthMeasureSpec, minimumWidth);
int computedHeight = resolveMeasured(heightMeasureSpec, minimumHeight); ... setMeasuredDimension(computedWidth, computedHeight);
}
@Override
protected int getSuggestedMinimumWidth() {
// View should be large enough to contain the background + handle and
// target drawable on either edge.
return (int) (Math.max(mOuterRing.getWidth(), 2 * mOuterRadius) + mMaxTargetWidth);
}
mOuterRing为一个指定的圆(由Shape画出,因此给出的是宽和高,相当于圆半径),mOuterRadius为最大的虚线圆的半径,mMaxTargetWidth为图中Zzz图片或其它图片的宽度。这部分值是有开发人员指定的自己期望的自己的视图中属性的大小
private int resolveMeasured(int measureSpec, int desired)
{
int result = 0;
int specSize = MeasureSpec.getSize(measureSpec);
switch (MeasureSpec.getMode(measureSpec)) {
case MeasureSpec.UNSPECIFIED:
result = desired;
break;
case MeasureSpec.AT_MOST:
result = Math.min(specSize, desired);
break;
case MeasureSpec.EXACTLY:
default:
result = specSize;
}
return result;
}
我们从width分析,承载这个View的ViewGroup可能有两种情况,一(A)提供的空间比上面我们给出的值大,二(B)是要小(开发中基本上我们不会允许这种状况出现,但设计的逻辑依然必须考虑到这种可能)。然后就要看的是View的layout_width,一是wrap_content,对应AT_MOST,A得到的值为给出的值,B值为父视图的值,虽然我们不希望这样,但父视图只给出了这么些空间,我们也只得这样。
二是match_parent,对应只有一个父视图的值,这也是符合要求的。
一个好的设计应该要考虑到各种情况下的使用,而一个可复用的框架设计更是如此,设计之前需要预想到各种可能的应用情况。
最新文章
- PHP写的异步高并发服务器,基于libevent
- 用JS控制图片随鼠标移动
- Picasso 加载图片到RelativeLayout之解决方案
- jstl 保留两位小数
- 三、spark入门:文本中发现5个最常用的word,排除常用停用词
- Robots协议
- GPUImage原理
- 团队项目beta 汇总
- 使用jquery-qrcode在页面上生成二维码,支持中文
- selenium + robotframework的运行原理
- theano安装问题
- 各操作系统安装redis
- Entity Frame Code First 简易教程
- 使用切片拦截Rest服务
- P1481 魔族密码 (LIS)
- Linux LVM逻辑卷配置过程详解(创建,增加,减少,删除,卸载)
- 关于datatables自适应以及自定义列宽度的总结
- 采用boosting思想开发一个解决二分类样本不平衡的多估计器模型
- 如何在Drupal7中用代码批量创建节点、评论和分类
- python之函数(function)
热门文章
- window下查杀占用端口的进程
- check the manual that corresponds to your MySQL server version for the right syntax to use near
- Post请求和Get请求;@RequestBody和@RequestParam
- [Processing]在画布上写文本
- 廖雪峰git教程学习笔记2
- webpack3升级为webpack4
- Vuejs 使用 lib 库模式打包 umd 解决 NPM 包发布的问题
- MUI的踩坑笔记
- 论文笔记:分形网络(FractalNet: Ultra-Deep Neural Networks without Residuals)
- c++ 使用this指针进行串联的函数调用