自定义控件都会去重写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    }

我是天王盖地虎的分割线

更多相关文章

  1. Xamarin.Android(安卓)自定义VideoView 进度条
  2. Android(安卓)View框架的measure机制
  3. android popupwindow的使用与讲解
  4. 【Android基础】常用的布局和单位的简介
  5. Android学习笔记16:Button控件图文混排效果的实现
  6. 判断Edittext是否为几种空方法
  7. Android关于滚动View标题栏渐变的解决方案
  8. android动画详解
  9. android之animation(一)

随机推荐

  1. Android学习笔记之TabHost
  2. Android(安卓)知识点梳理,较完整
  3. Android单元测试(一)
  4. android studio ndk开发遇到的问题
  5. android 可执行程序终于可以在nxp8473--c
  6. android Ant批打包学习(一):生成没有签名
  7. Android 发展史
  8. Android快速开发02之仿京东底部Tab
  9. ANDROID Porting系列三、Build Cookbook
  10. Android 应用语言设置的实现