一起Talk Android吧(第一百四十一回:Android自定义View之Measure五)

各位看官们,大家好,上一回中咱们说的是Android中自定义View之Measure的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在前面章回中介绍了MeasureSpec的细节,并且介绍了View是如何获取MeasuraSpec的,在本章回中我们将介绍View在Measure过程中如何使用MeasureSpec的。

我们在前面章回介绍过Measure的主要流程:
performMeasurea()->measure()->onMeasure(),
在这里我还需要把这个流程做一点小小的完善:
doTraversal()->performTraversals()->performMeasurea()->measure()->onMeasure().

该流程中前三个函数在ViewRootImpl.java文件中,后两个函数在View.java文件中。其原因我们在119回中介绍过,如果忘记的话可以回去看看.前两个函数没有参数,后三个函数的参数都一样,而且都是MeasureSpec标准的变量,那么这些MeasureSpec标准的变量是怎么来的呢,我们看一下performTraversals()函数的源代码就明白:(这里忽略了大部分的内容,只列出了与问题相关的代码)

  int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
  int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);

   // Ask host how big it wants to be
  performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);

从上面的源代码可以看到MeasureSpec标准的变量是通过getRootMeasureSpec()函数获取到的,我们在上一回中专门分析过该函数的源代码,相信大家还记得,因此我们就不再介绍了,接下来我们直接看View类中Measure()方法和onMeasure()方法的源代码就可以。这才是本章回的主题。刚才说的这些都是引子呀,不过这个引子把前面章回的很多知识都连起来了,引的好,好引子。有看官突然说了这么一段,不过他说的完全正确。“说的好!”我也要给他个好评!

看官们,我们先看看Measure()方法的源代码:

 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
     boolean optical = isLayoutModeOptical(this);
     if (optical != isLayoutModeOptical(mParent)) {
         Insets insets = getOpticalInsets();
         int oWidth  = insets.left + insets.right;
         int oHeight = insets.top  + insets.bottom;
         widthMeasureSpec  = MeasureSpec.adjust(widthMeasureSpec,  optical ? -oWidth  : oWidth);
         heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
     }

     // Suppress sign extension for the low bytes
     long key = (long) widthMeasureSpec << 32 | (long) heightMeasureSpec & 0xffffffffL;
     if (mMeasureCache == null) mMeasureCache = new LongSparseLongArray(2);

     final boolean forceLayout = (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;

     // Optimize layout by avoiding an extra EXACTLY pass when the view is
     // already measured as the correct size. In API 23 and below, this
     // extra pass is required to make LinearLayout re-distribute weight.
     final boolean specChanged = widthMeasureSpec != mOldWidthMeasureSpec
             || heightMeasureSpec != mOldHeightMeasureSpec;
     final boolean isSpecExactly = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY
             && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY;
     final boolean matchesSpecSize = getMeasuredWidth() == MeasureSpec.getSize(widthMeasureSpec)
             && getMeasuredHeight() == MeasureSpec.getSize(heightMeasureSpec);
     final boolean needsLayout = specChanged
             && (sAlwaysRemeasureExactly || !isSpecExactly || !matchesSpecSize);

     if (forceLayout || needsLayout) {
         // first clears the measured dimension flag
         mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;

         resolveRtlPropertiesIfNeeded();

         int cacheIndex = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
         if (cacheIndex < 0 || sIgnoreMeasureCache) {
             // measure ourselves, this should set the measured dimension flag back
             onMeasure(widthMeasureSpec, heightMeasureSpec);
             mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
         } else {
             long value = mMeasureCache.valueAt(cacheIndex);
             // Casting a long to int drops the high 32 bits, no mask needed
             setMeasuredDimensionRaw((int) (value >> 32), (int) value);
             mPrivateFlags3 |= PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
         }

         // flag not set, setMeasuredDimension() was not invoked, we raise
         // an exception to warn the developer
         if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
             throw new IllegalStateException("View with id " + getId() + ": "
                     + getClass().getName() + "#onMeasure() did not set the"
                     + " measured dimension by calling"
                     + " setMeasuredDimension()");
         }

         mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
     }

     mOldWidthMeasureSpec = widthMeasureSpec;
     mOldHeightMeasureSpec = heightMeasureSpec;

     mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 |
             (long) mMeasuredHeight & 0xffffffffL); // suppress sign extension
 }

代码中可以看到它主要是依据背景或者其它特殊情况对传递来的MeasureSpec标准变量进行调整,主要的功能是通过onMeasure()方法实现,我们接下来看看该方法的源代码:

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
             getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
 }

代码中会调用其它函数设置View的长宽属性,这里不一一列出,大家可以自己去分析,最终的结果就是给Viwe的成员变量赋值,以便在其它地方使用,具体的代码如下:

mMeasuredWidth = measuredWidth;    //给Viewr的成员变量赋值
mMeasuredHeight = measuredHeight;  //给Viewr的成员变量赋值

最后做个总结,符合MeasureSpec标准的变量通过流程中的层层传递最后赋值给了View中的成员变量,这些变量代表着View的长度和宽度。

各位看官,关于Androd中自定义View之Measure的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!

发布了520 篇原创文章 · 获赞 131 · 访问量 62万+

猜你喜欢

转载自blog.csdn.net/talk_8/article/details/98454176