美文网首页
自定义View相关

自定义View相关

作者: 寺雨九歌 | 来源:发表于2020-10-27 09:51 被阅读0次

View#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
    }
  1. isLayoutModeOptical(Object)
    光学边界( Optical bounds):
    Optical bounds describe where a widget appears to be. They sit inside the clip bounds which need to cover a larger area to allow other effects, such as shadows and glows, to be drawn.
    光学边界描述小部件的位置。它们位于剪辑范围内,需要覆盖更大的区域,以便绘制其他效果,如阴影和发光。

相关文章

  • 自定义View——切换按钮

    自定义View的步骤 今天来介绍下如何自定义类似iOS的按钮。 要想自定义View,需要了解自定义View的相关步...

  • 自定义View

    4.4 自定义View 本节将详细介绍自定义View相关的知识。自定义View的作用不用多说,这个读者都应该清楚,...

  • 安卓资料汇总

    目录 Kotlin Rxjava 自定义View WebView 图片相关 ViewPager相关 设计模式 An...

  • Android自定义View——布局Layout

    之前写了一篇文章介绍自定义View,主要是介绍了自定义View绘制相关的操作。这里主要是介绍自定义View另一个重...

  • 重新设计 Android View 体系?

    作者:leobert-lan 关于 Android View 体系,大家从如何自定义 View 到相关源码的掌握,...

  • Java Android技术学习以及面试

    点我跳转github查看 目录 Android基础进阶Gradle相关自定义View插件化相关热修复相关编译器相关...

  • Android View(转)

    自定义View的原理自定义View基础 - 最易懂的自定义View原理系列自定义View Measure过程 - ...

  • 开发笔记,优化TableView 0.00001

    聊天流显示性能提高 前言:徽章是一个自定义view,生成相关的model后,赋值给view,然后把view放入YY...

  • 自定义view相关

    ####### 事实证明 不管是自定义viewgroup 还是 view ontouchevent默认都是返回tr...

  • 自定义View相关

    View#measure isLayoutModeOptical(Object)光学边界( Optical bou...

网友评论

      本文标题:自定义View相关

      本文链接:https://www.haomeiwen.com/subject/dtzmjctx.html