View的绘制流程

Android自定义View Android自定义ViewAndroid自定义View概述Android开发进阶的必经之路为什么要自定义View自定义View的基本方法自定义控件分类自定义View基础View的分类View类简介AttributeSet与自定义属性View视图结构Android坐标系View位置(坐标)描述位置获取方式Android中颜色相关内容View树的绘制流程View树的绘制流程是谁负责的?view的添加view的绘制流程measurelayoutdrawLayoutParamsMarginLayoutParams 阅读详情

一、View的绘制基础

  1. MeasureSpace
    Android官方给的解释MeasureSpecs are implemented as ints to reduce object allocation. This class is provided to pack and unpack the <size, mode> tuple into the int.

MeasureSpecs的是为了减少int内存分配。提供这个类是为了将<Size, Mode>元组打包和解包到一个int中。

MeasureSpecs代表一个int值,高2位SpeceMode(测量模式),低30位代表SpecSize(测量模式下的数值大小)

private static final int MODE_SHIFT = 30;//偏移量
private static final int MODE_MASK  = 0x3 << MODE_SHIFT;
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
public static final int EXACTLY     = 1 << MODE_SHIFT;
public static final int AT_MOST     = 2 << MODE_SHIFT;

//size和mode合为一个int值
public static int makeMeasureSpec(int size, int mode) {
      if (sUseBrokenMakeMeasureSpec) {
          return size + mode;
      } else {
          return (size & ~MODE_MASK) | (mode & MODE_MASK);
      }
    }
 //获取模式   
public static int getMode(int measureSpec) {
            //noinspection ResourceType
            return (measureSpec & MODE_MASK);
        }

//获取大小
public static int getSize(int measureSpec) {
            return (measureSpec & ~MODE_MASK);
        }

makeMeasureSpec将mode和size打包成一个int

UNSPECIFIED

父容器ViewGroup不对子View施加任何约束,子View可以是任意大小。

EXACTLY

父容器决定子View的确切大小,父容器要求子View必须严格按照它给定的值来约束自己。对应于LayoutParams中的match_parant和具体的数值。

AT_MOST

在父容器指定的范围内,子View可以任意大小。对应于LayoutParams中的wrap_content

2.Activity、Window、ViewRoot、DecorView的关系图
在这里插入图片描述

类型定义作用
ViewRoot连接器连接WindowManager和DecorView, 完成View的绘制:measure layout draw
DecorView顶级View显示和加载布局
Window承载器承载视图View
Activity控制器控制生命周期和处理使事件

3、ViewRoot和DecorView

ViewRoot对应于ViewRootImpl类,它是连接Window和DecorView的纽带,View的measure、layout、draw都是在viewRoot中完成的。View的绘制流程从ViewRoot的performTraversals方法开始,分别经过measure、layout和draw,其中measure用来确定控件的宽高,layout用来确定view在父容器中的位置,而draw则负责将View绘制在屏幕上。performTraversals会调用performMeasure、performLayout和performDraw三个方法,这三个方法完成了UI显示的三个要素尺寸大小、位置和内容。

  • performMeasure(尺寸大小)

    用于计算View对象在UI界面上绘图区域的大小

  • performLayout(位置)

用于计算View对象在UI界面上的位置

  • performdraw(绘制)

大小和位置确定后,View对象就可以在此基础上绘制UI内容

二、源码分析View绘制流程
1、performMeasure

private void performTraversals() {
    ...
    //mStopped Activity处于stop状态,对应的Window也会处于stopped
    if (!mStopped || mReportNextDraw) {
                boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
                        (relayoutResult&WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0);
                if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
                        || mHeight != host.getMeasuredHeight() || contentInsetsChanged ||
                        updatedConfiguration) {
                    //DecorView宽度
                    int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
                    //DecorView高度
                    int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
                    // Ask host how big it wants to be
                    performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
}
 private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
        if (mView == null) {
            return;
        }
        try {
            mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_VIEW);
        }
    }

条件一旦满足performTraversals就会调用performMeasure(),这个函数没有做什么只调用了mView.measure()方法,mView为DecorView的实例。这样ViewRootImpl就将控制权交给了View的根元素,真正的Traversal才刚刚开始。measure()是不能被子类重载的,因为在View类中measure()是final类型。真正的测量是在onMeasure()中进行的,View中提供了onMeasure()的默认实现。

public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
    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;
            }

}
//View中的默认实现
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }
    
 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
        boolean optical = isLayoutModeOptical(this);
        if (optical != isLayoutModeOptical(mParent)) {
            Insets insets = getOpticalInsets();
            int opticalWidth  = insets.left + insets.right;
            int opticalHeight = insets.top  + insets.bottom;

            measuredWidth  += optical ? opticalWidth  : -opticalWidth;
            measuredHeight += optical ? opticalHeight : -opticalHeight;
        }
        setMeasuredDimensionRaw(measuredWidth, measuredHeight);
    }
    
private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
        mMeasuredWidth = measuredWidth;
        mMeasuredHeight = measuredHeight;

        mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
    }

虽然View.java提供了默认的onMeasure实现,但是扩展的视图通常都需要考虑自己的因素,所以会选择重载onMeasure来满足自己的需求。比如DecorView继承FrameLayout,其onMeasure()源码如下所示:

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 //获取子View的个数
        int count = getChildCount();
//判断父View的mode是否为EXACTLY,如果宽和高其中一个不是则为true
        final boolean measureMatchParentChildren =
                MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
                MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
        mMatchParentChildren.clear();

        int maxHeight = 0;//所有子View测量的最大高度
        int maxWidth = 0;//所有子View测量的最大宽度
        int childState = 0;

        for (int i = 0; i < count; i++) {//循环处理子View
            final View child = getChildAt(i);//获取子View
            if (mMeasureAllChildren || child.getVisibility() != GONE) {
               //确定子Viw的宽高
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                //获取最大值
                maxWidth = Math.max(maxWidth,
                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                childState = combineMeasuredStates(childState, child.getMeasuredState());
                if (measureMatchParentChildren) {
                    if (lp.width == LayoutParams.MATCH_PARENT ||
                            lp.height == LayoutParams.MATCH_PARENT) {
                        mMatchParentChildren.add(child);
                    }
                }
            }
        }

        // Account for padding too
        maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
        maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();

        // Check against our minimum height and width
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

        // Check against our foreground's minimum height and width
        final Drawable drawable = getForeground();
        if (drawable != null) {
            maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
            maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
        }

       //存储测量结果,可以通过getMeasuredWidth(),getMeasuredHeight() 获取
        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));
    //上面的流程确定了当前FrameLayout的宽高,因此子View中MODE为非AT_MOST的需要根据当前FrameLayout的宽或高重新测量
    count = mMatchParentChildren.size();
        if (count > 1) {
            for (int i = 0; i < count; i++) {
                final View child = mMatchParentChildren.get(i);
                final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

                final int childWidthMeasureSpec;
                if (lp.width == LayoutParams.MATCH_PARENT) {
                    final int width = Math.max(0, getMeasuredWidth()
                            - getPaddingLeftWithForeground() - getPaddingRightWithForeground()
                            - lp.leftMargin - lp.rightMargin);
                    childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                            width, MeasureSpec.EXACTLY);
                } else {
                    childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                            getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
                            lp.leftMargin + lp.rightMargin,
                            lp.width);
                }

                final int childHeightMeasureSpec;
                if (lp.height == LayoutParams.MATCH_PARENT) {
                    final int height = Math.max(0, getMeasuredHeight()
                            - getPaddingTopWithForeground() - getPaddingBottomWithForeground()
                            - lp.topMargin - lp.bottomMargin);
                    childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                            height, MeasureSpec.EXACTLY);
                } else {
                    childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                            getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
                            lp.topMargin + lp.bottomMargin,
                            lp.height);
                }

                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }
        
    }

measureChildWithMargins()为ViewGoup中的方法,此方法会调用子View中的measure()方法,源码如下:

protected void measureChildWithMargins(View child,
            int parentWidthMeasureSpec, int widthUsed,
            int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                        + heightUsed, lp.height);

       //调用View中的measure方法
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

总结:

变量count代表FrameLayout中包含的子View的数量,它记录在ViewGroup.mChildren的数组中。基于ViewTree的特性,我们需要逐个处理这些字View,通过调用measureChildWithMargins,意为需要考虑padding和margins。此函数会根据padding和margin来生成新的spec,然后调用child.measure()函数,于是遍历流程就到子View中。此View如果为ViewGroup还会调用到重载的onMeasure方法,依次“递归”下去,直到叶节点。

2.performLayout

通过上面的performMeasure,ViewTree的元素大小基本确定下来,并保存在自己的变量 mMeasuredWidth, mMeasuredHeight中。接下来ViewRootImpl会进入performLayout来确定位置测量。Android官方给的Layout解释为:

“Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure(int, int) and is a top-down traversal of the view tree… The second pass happens in layout(int, int, int, int) and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.”

通过源码分析layout的过程

private void performTraversals() {
  ...
     final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw);
        boolean triggerGlobalLayoutListener = didLayout
                || mAttachInfo.mRecomputeGlobalAttributes;
        if (didLayout) {
            performLayout(lp, mWidth, mHeight);
    ...
}

一旦ViewRootImpl发现需要执行layout,它就会调用performLayout进行位置测量。在performLayout中会调用顶层View(mView)的layout。此时递归调用ViewTree中的各个View的过程才开始。

 private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
            int desiredWindowHeight) {
   final View host = mView;
        if (host == null) {
            return;
        }
      
        try {
            host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());             
            }
    ...

以FrameLayout为例看layout的遍历过程

View.java

public void layout(int l, int t, int r, int b) {
     boolean changed = isLayoutModeOptical(mParent) ?
                setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);

        if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
            onLayout(changed, l, t, r, b);
}

函数参数l、t、r、b分别代表此View对象左上和右下边框的坐标,layout通过setFrame()直接将这些值记录在成员变量中,即mLeft、mTop、mRight和mBottom,确定了当前View的位置。因此onLayout函数实体是空的,这就要求各个ViewGroup扩展类,如FrameLayout重载并具体实现它们所需的功能。

FrameLayout.java的onLayout实现

 @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        layoutChildren(left, top, right, bottom, false /* no force left gravity */);
    }

    void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
        final int count = getChildCount();

        //边框距离内容区的距离
        final int parentLeft = getPaddingLeftWithForeground();
        final int parentRight = right - left - getPaddingRightWithForeground();

        final int parentTop = getPaddingTopWithForeground();
        final int parentBottom = bottom - top - getPaddingBottomWithForeground();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();

                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();

                int childLeft;
                int childTop;

                int gravity = lp.gravity;
                if (gravity == -1) {
                    gravity = DEFAULT_CHILD_GRAVITY;
                }

                final int layoutDirection = getLayoutDirection();
                final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
                final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

                switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                 //child水平居中计算child的左边界坐标
                    case Gravity.CENTER_HORIZONTAL:
                        childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
                        lp.leftMargin - lp.rightMargin;
                        break;
                    //child居右计算child的左边界坐标
                    case Gravity.RIGHT:
                        if (!forceLeftGravity) {
                            childLeft = parentRight - width - lp.rightMargin;
                            break;
                        }
                    //child居左计算child的左边界坐标
                    case Gravity.LEFT:
                    default:
                        childLeft = parentLeft + lp.leftMargin;
                }

                switch (verticalGravity) {
                    case Gravity.TOP:
                        childTop = parentTop + lp.topMargin;
                        break;
                    case Gravity.CENTER_VERTICAL:
                        childTop = parentTop + (parentBottom - parentTop - height) / 2 +
                        lp.topMargin - lp.bottomMargin;
                        break;
                    case Gravity.BOTTOM:
                        childTop = parentBottom - height - lp.bottomMargin;
                        break;
                    default:
                        childTop = parentTop + lp.topMargin;
                }

               //
                child.layout(childLeft, childTop, childLeft + width, childTop + height);
            }
        }
    }

layout的递归过程和measure的递归过程一样,直到根View为止。

3、performDraw
一个对象的layout确定后,它才能在此基础上执行Draw。performDraw是遍历流程中最后被调用的,将在“画板”上产生UI数据,然后在适当的时机有SurfaceFlinger进行整合,最终显示到屏幕上。绘制UI的实现核心如下:

  • Surface
  • 图形绘制的方式(绘制方式两种,即硬件和软件)
  • View Tree各元素的协调关系
private void performTraversals() {
    ...
    if (!cancelDraw) {
            if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
                for (int i = 0; i < mPendingTransitions.size(); ++i) {
                    mPendingTransitions.get(i).startChangingAnimations();
                }
                mPendingTransitions.clear();
            }

            performDraw();
    ...
}

软件渲染方式:

private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
            boolean scalingRequired, Rect dirty, Rect surfaceInsets) {

        // Draw with software renderer.
        final Canvas canvas;

        // We already have the offset of surfaceInsets in xoff, yoff and dirty region,
        // therefore we need to add it back when moving the dirty region.
        int dirtyXOffset = xoff;
        int dirtyYOffset = yoff;
        if (surfaceInsets != null) {
            dirtyXOffset += surfaceInsets.left;
            dirtyYOffset += surfaceInsets.top;
        }

        try {
            dirty.offset(-dirtyXOffset, -dirtyYOffset);
            final int left = dirty.left;
            final int top = dirty.top;
            final int right = dirty.right;
            final int bottom = dirty.bottom;

            canvas = mSurface.lockCanvas(dirty);

            // TODO: Do this in native
            canvas.setDensity(mDensity);
        } catch (Surface.OutOfResourcesException e) {
            handleOutOfResourcesException(e);
            return false;
        } catch (IllegalArgumentException e) {
            Log.e(mTag, "Could not lock surface", e);
            // Don't assume this is due to out of memory, it could be
            // something else, and if it is something else then we could
            // kill stuff (or ourself) for no reason.
            mLayoutRequested = true;    // ask wm for a new surface next time.
            return false;
        } finally {
            dirty.offset(dirtyXOffset, dirtyYOffset);  // Reset to the original value.
        }

        try {
            // If this bitmap's format includes an alpha channel, we
            // need to clear it before drawing so that the child will
            // properly re-composite its drawing on a transparent
            // background. This automatically respects the clip/dirty region
            // or
            // If we are applying an offset, we need to clear the area
            // where the offset doesn't appear to avoid having garbage
            // left in the blank areas.
            if (!canvas.isOpaque() || yoff != 0 || xoff != 0) {
                canvas.drawColor(0, PorterDuff.Mode.CLEAR);
            }

            dirty.setEmpty();
            mIsAnimating = false;
            mView.mPrivateFlags |= View.PFLAG_DRAWN;
            canvas.translate(-xoff, -yoff);
            if (mTranslator != null) {
                mTranslator.translateCanvas(canvas);
            }
            canvas.setScreenDensity(scalingRequired ? mNoncompatDensity : 0);

            mView.draw(canvas);

            drawAccessibilityFocusedDrawableIfNeeded(canvas);
        } finally {
            try {
                surface.unlockCanvasAndPost(canvas);
            } catch (IllegalArgumentException e) {
                Log.e(mTag, "Could not unlock surface", e);
                mLayoutRequested = true;    // ask wm for a new surface next time.
                //noinspection ReturnInsideFinallyBlock
                return false;
            }
        }
        return true;
    }
  • lockCanvas Canvas的底层实现仍然是Surface,在使用Canvas前,必须显式地锁定它,然后才能正常使用。
  • 坐标变换。包括由入参传入的yoff组成的坐标平移,以及mTranslator指示的变换。
  • View.draw 是真正绘制UI的地方
  • unlockCanvasAndPost 目前改变的只是本地数据,只有把draw完成后的Canvas信息透过Surface提交给SurfaceFlinger,并有后者统一合成渲染到FrameBuffer中,才能最终把界面显示到屏幕上。
public void draw(Canvas canvas) {
        final int privateFlags = mPrivateFlags;
        mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

        /*
         * Draw traversal performs several drawing steps which must be executed
         * in the appropriate order:
         *
         *      1. Draw the background  绘制背景
         *      2. If necessary, save the canvas' layers to prepare for fading  保存Canvas的layers,以备后续fading所需
         *      3. Draw view's content 绘制内容区
         *      4. Draw children 绘制子对象
         *      5. If necessary, draw the fading edges and restore layers 绘制fading,restore第(2)步保存的layers
         *      6. Draw decorations (scrollbars for instance) 绘制decoration(主要scrollbars)
         */

        // Step 1, draw the background, if needed
        int saveCount;

        drawBackground(canvas);

        // skip step 2 & 5 if possible (common case)//horizontalEdges verticalEdges为false跳过2和5步直接进入第3步
        final int viewFlags = mViewFlags;
        boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
        boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
        if (!verticalEdges && !horizontalEdges) {
            // Step 3, draw the content
            onDraw(canvas);

            // Step 4, draw the children
            dispatchDraw(canvas);

            drawAutofilledHighlight(canvas);

            // Overlay is part of the content and draws beneath Foreground
            if (mOverlay != null && !mOverlay.isEmpty()) {
                mOverlay.getOverlayView().dispatchDraw(canvas);
            }

            // Step 6, draw decorations (foreground, scrollbars)
            onDrawForeground(canvas);

            // Step 7, draw the default focus highlight
            drawDefaultFocusHighlight(canvas);

            if (debugDraw()) {
                debugDrawFocus(canvas);
            }

            // we're done...
            return;
        }

当前view自身的绘制是通过onDraw(canvas),View绘制过程的传递是通dispatchDraw(canvas)来实现的,dispatchDraw会通过遍历调用所有子元素的draw方法。如此递归下去直到根元素。

protected void dispatchDraw(Canvas canvas) {
    for (int i = 0; i < childrenCount; i++) {
            while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
                final View transientChild = mTransientViews.get(transientIndex);
                if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
                        transientChild.getAnimation() != null) {
                    more |= drawChild(canvas, transientChild, drawingTime);
                }
                transientIndex++;
                if (transientIndex >= transientCount) {
                    transientIndex = -1;
                }
            }

            final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
            final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
                more |= drawChild(canvas, child, drawingTime);
            }
        }
}
 protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        return child.draw(canvas, this, drawingTime);
    }
手把手教你用STM32F103C8T6单片机,做一个即插即用的USB游戏手柄(附完整代码) 本文详细介绍了如何使用STM32F103C8T6单片机制作即插即用的USB游戏手柄,涵盖硬件搭建、HID协议解析及完整代码实现。通过深入讲解USB HID协议的七大关键描述符,帮助开发者快速掌握嵌入式USB设备开发技巧,打造个性化游戏控制器。 阅读详情

相关推荐

深入Linux内核:PTP硬件时间戳(HW Timestamp)是如何炼成的?

本文深入探讨了Linux内核中PTP硬件时间戳(HW Timestamp)的实现原理与实战应用。通过分析IEEE 1588协议在网络时间同步中的关键作用,详细解析了硬件时间戳在Linux内核中的架构设计、驱动开发及性能优化技巧,帮助开发者实现纳秒级精度的时间同步。

weixin_30727835的博客 664

小程序开发基础-view视图容器

view 视图容器。 // wxml <view class="section"> <view class="section__title">flex-direction: row</view> <view class="flex-wrp_one"> <view class="flex-item bc_green">1</view> <view class="flex-item bc_red"&...

qq_35382207的博客 1252

【Flink】状态编程: 订单超时告警

文章目录一、基础概念二、Flink状态编程1、支持的状态类型2、Managed Keyed State六种类型基本API状态的生命周期3、Managed Operator State三、案例:订单延迟告警统计1、需求描述2、需求分析3、数据与模型4、详细实现 一、基础概念 在Flink架构体系中,有状态计算可以说是Flink非常重要的特性之一。 有状态计算是指: 在程序计算过程中,在Flink...

一只皮皮熊 2875

View的简介

认识一个新的事物,首先我们从概念上讲,我们需要知道,这个事物 是什么,这个事物有什么用途? 对应到View 上,我们要搞明白 View 的定义以及工作原理。  1.View是什么?   View是屏幕上的一块矩形区域,它负责用来显示一个区域,并且响应这个区域内的事件。可以说,手机屏幕上的任意一部分看的见得地方都是View,它很常见,比如 TextView 、ImageView 、Button

编程之美 1万+

嘉立创EDA报错:元件引脚与焊盘未对应报错解决记录

原件引脚与焊盘未对应报错解决方法,一文总结!

2303_77013775的博客 1万+

【CANoe16安装教程】亲测安装成功

CANoe16安装教程

qq_45302638的博客 2万+

微信小程序View组件全解析与实战指南

微信小程序的 <view> 组件是基础的视图容器,类似于 HTML 中的 <div>,用于布局和样式控制。它默认为块级元素,支持 Flex 布局,并提供了多种属性和事件处理功能。通过 hover-class 可以实现点击反馈,scroll-top 控制滚动位置,disable-scroll 阻止滚动事件冒泡。示例代码展示了 <view> 的基本使用、Flex 布局和事件处理。开发时需注意 <view> 的默认行为和属性设置,合理使用其功能可以提升用户体验。建议

爱的叹息的专栏 1500

芯片测试中的DC与AC模式详解:如何选择最适合你的测试方案?

本文深入解析了芯片测试中DC(直流)与AC(交流)模式的核心差异与互补性。DC测试关注静态电气特性,如固定型故障;而AC测试则聚焦于芯片在额定工作速度下的动态时序行为。文章从VLSI设计、电路模块、成本约束、可测试性设计和应用场景五大维度,提供了如何根据工艺节点、工作频率及具体需求,在testMode配置、时钟复位策略和输入输出接口设计中,平衡两种测试模式以制定最优方案的决策框架。

bb456的博客 1096

微信小程序学习之路——组件定义及属性(view组件)

小程序定义了各种各样的组件,它们在WXML中起着各不同的作用。与HTML元素一样,一个组件是指从组件开始标签到结束标签的所有代码,由于组件可能会被转译为不同端对应的代码,所以在页面创建过程中,不能使用小程序组件标签以外的标签。 组件定义及属性 1)组件是视图层的基本组成单元 2)组件自带一些功能与微信风格的样式 3)一个组件通常包括开始标签和结束标签,属性用来修饰这个组件,内容在两个标签之...

创作星球 2万+

完整的 Python 数据分析案例:新能源汽车续航里程预测

随着新能源汽车的普及,续航里程成为消费者关注的重要指标。准确预测新能源汽车的续航里程,有助于车企优化电池管理系统、提升车辆性能,也能帮助消费者合理规划出行。本案例将基于新能源汽车的各项参数,如电池容量、行驶速度、环境温度等,构建续航里程预测模型。通过以上改进,可以提高数据处理的准确性、模型的性能和可视化的效果。

daqsdfas的博客 1596

CMake入门教程【基础篇】CMake+Visual Studio2022构建C++项目

CMake+Visual Studio2022搭建C++环境

小鱼酱的博客 4980

Android View绘制流程

前言     在学习安卓的时候免不了和布局文件打交道,一直都想弄清楚内部究竟是个什么原理,于是有了这篇文章。     这篇文章形成与多个大神的文章集和,算是帮大家简单过滤一下关键信息,相信看完后能够基本上了解绘制流程是个什么样子。(无安卓基础的话可能有点难懂hhh) View绘制流程     View 的绘制流程分为三步:在自定义View的时候一般需要重写父类的onMeasure()、onLayout()、onDraw()三个方法,来完成视图的展示过程。当然,这三个暴露给开发者重写的方法只不过是整个绘制流程

weixin_41607932的博客 1万+

华为OD机试真题 新系统 9月16号 【统计特殊数字】

华为OD新系统机试真题【统计特殊数字】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【统计特殊数字】。

qq_45776114的博客 810

View和ViewGroup,View绘制流程解析

View是绘制在屏幕上的用户能与之交互的一个对象,它代表了用户界面组件的一块可绘制的空间块,每一个空间块在屏幕上占据一个长方 形区域。View 的绘制流程是从 ViewRootImpl 的 performTraversals() 方法开始的,它经过测量(measure)、放置(layout)和绘制(draw)三个过程才能把一个 View 绘制出来,measure() 方法用于测量 View 的宽高,layout() 用于确定 View 在父容器中的放置位置,draw() 负责做具体的绘制操作。

qq_45730283的博客 1406

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

【Android】浅谈View绘制流程

measureSpec是一个int类型,在java占32字节,其1~30位表示实际测量长度,31~32位即测量模式通过MODE_MASK获得mode,MODE_MASK 即。

qq_40900926的博客 922

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

谈谈 View 绘制流程

注:本文使用 sdk 23 作为源码参考。 前言 ViewRootImpl#performTraversals() ViewRootImpl#performMeasure() ViewRootImpl#performLayout() ViewRootImpl#performDraw() some tips onDraw() dispatchDraw() 前言 关于 View 的绘制流

joker 880

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Android中View的绘制流程

最近在看一个开源项目的时候,发

slowalker 738

view的绘制流程

一、view树的绘制流程 measure--->layout--->draw measure 1、ViewGroup.LayoutParams 指定部件的长宽 2、MeasureSpec 32位的int值 前两位代表模式 后30位测量规格的大小 layout draw invalidate()请求android系统 如果大小没有发生变化 就不会调用layout放置这个过程 requestLayout() 当布局发生变化时 希望重新测量尺寸大小 就会触发 mea

3976

Android View的绘制流程

前言:为什么我们要了解View的绘制流程?在回答这个问题前,想必大家都知道自定义View吧,如果将这两者联系起来,答案也就清楚了:了解View的基本绘制流程对我们自定义View是很有必要的。 一、绘制入口 View绘制的入口是在ViewRootImpl.java的performTraversals()方法中,主要代码如下: private void performTraversals() ...

不将就 283

View 绘制流程

View 触摸机制(事件分发)View 绘制流程

Android 689

View 的绘制流程

View 的绘制流程 View 的绘制流程其实就是 View 的 measure\layout\draw 过程,分别对应测量、布局和绘制。 ViewRoot ViewRoot 对于 ViewRootImpl 类,他是连接 WindowManager 和 DecorView 的桥梁。View 的三大流程都是通过 ViewRoot 完成的。在 ActivityThread 类中,activity 创建...

okclouderx的博客 190
上一篇: 终结任务
下一篇: View事件分发
coderPlus
博客等级 码龄12年 1粉丝 24原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址