Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android View 默認的onMeasure

Android View 默認的onMeasure

編輯:高級開發

lp 其實就是上個博客提到的Window Attribute 只不過是VIEwRoot的。

  Java代碼

  childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);

  childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);

  desiredWindowWidth 根據代碼可知,一般情況就是屏幕的寬度;desiredWindowHeight 根據代碼可知,一般情況就是屏幕的寬度-狀態欄高度;

  /**

  * Figures out the measure spec for the root vIEw in a window based on it's

  * layout params.

  *

  * @param Windowsize

  * The available width or height of the window

  *

  * @param rootDimension

  * The layout params for one dimension (width or height) of the

  * window.

  *

  * @return The measure spec to use to measure the root vIEw.

  */

  private int getRootMeasureSpec(int Windowsize, int rootDimension) {

  int measureSpec;

  switch (rootDimension) {

  case VIEwGroup.LayoutParams.MATCH_PARENT:

  // Window can't resize. Force root vIEw to be Windowsize.

  measureSpec = MeasureSpec.makeMeasureSpec(Windowsize, MeasureSpec.EXACTLY);

  break;

  case VIEwGroup.LayoutParams.WRAP_CONTENT:

  // Window can resize. Set max size for root vIEw.

  measureSpec = MeasureSpec.makeMeasureSpec(Windowsize, MeasureSpec.AT_MOST);

  break;

  default:

  // Window wants to be an exact size. Force root vIEw to be that size.

  measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);

  break;

  }

  return measureSpec;

  }

  host.measure(childWidthMeasureSpec, childHeightMeasureSpec);

  childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);

  childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight,

  接上頁

lp.height);

  desiredWindowWidth 根據代碼可知,一般情況就是屏幕的寬度;desiredWindowHeight 根據代碼可知,一般情況就是屏幕的寬度-狀態欄高度;

  /**

  * Figures out the measure spec for the root vIEw in a window based on it's

  * layout params.

  *

  * @param Windowsize

  * The available width or height of the window

  *

  * @param rootDimension

  * The layout params for one dimension (width or height) of the

  * window.

  *

  * @return The measure spec to use to measure the root vIEw.

  */

  private int getRootMeasureSpec(int Windowsize, int rootDimension) {

  int measureSpec;

  switch (rootDimension) {

  case VIEwGroup.LayoutParams.MATCH_PARENT:

  // Window can't resize. Force root vIEw to be Windowsize.

  measureSpec = MeasureSpec.makeMeasureSpec(Windowsize, MeasureSpec.EXACTLY);

  break;

  case VIEwGroup.LayoutParams.WRAP_CONTENT:

  // Window can resize. Set max size for root vIEw.

  measureSpec = MeasureSpec.makeMeasureSpec(Windowsize, MeasureSpec.AT_MOST);

  break;

  default:

  // Window wants to be an exact size. Force root vIEw to be that size.

  measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);

  break;

  }

  return measureSpec;

  }

  host.measure(childWidthMeasureSpec, childHeightMeasureSpec);

  注意ViewRoot並不是View,不過是ViewParent。刷新屏幕都是在這個類中,host就是PhoneWindow的DecorVIEw。

  Java代碼

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

  setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),

  getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));

  }

  /**

  接上頁

  * Utility to return a default size. Uses the supplIEd size if the

  * MeasureSpec imposed no contraints. Will get larger if allowed

  * by the MeasureSpec.

  *

  * @param size Default size for this vIEw

  * @param measureSpec Constraints imposed by the parent

  * @return The size this vIEw should be.

  */

  public static int getDefaultSize(int size, int measureSpec) {

  int result = size;

  int specMode = MeasureSpec.getMode(measureSpec);

  int specSize = MeasureSpec.getSize(measureSpec);

  switch (specMode) {

  case MeasureSpec.UNSPECIFIED:

  result = size;

  break;

  case MeasureSpec.AT_MOST:

  case MeasureSpec.EXACTLY:

  result = specSize;

  break;

  }

  return result;

  }

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

  setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),

  getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));

  }

  /**

  * Utility to return a default size. Uses the supplIEd size if the

  * MeasureSpec imposed no contraints. Will get larger if allowed

  * by the MeasureSpec.

  *

  * @param size Default size for this vIEw

  * @param measureSpec Constraints imposed by the parent

  * @return The size this vIEw should be.

  */

  public static int getDefaultSize(int size, int measureSpec) {

  int result = size;

  int specMode = MeasureSpec.getMode(measureSpec);

  int specSize = MeasureSpec.getSize(measureSpec);

  switch (specMode) {

  case MeasureSpec.UNSPECIFIED:

  result = size;

  break;

  case MeasureSpec.AT_MOST:

  case MeasureSpec.EXACTLY:

  result = specSize;

  break;

  }

  return result;

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved