View Focus的处理过程及ViewGroup的mFocused字段分析

  通过上篇的介绍,我们知道在对KeyEvent的处理中有非常重要的一环,那就是KeyEvent在focus view的path上自上而下的分发,

换句话说只有focus的view才有资格参与KeyEvent的处理,所以说focused view在KeyEvent的处理中很重要,我们需要弄清楚明白

focus view是如何设置以及改变的。

  通过Android官方文档http://developer.android.com/reference/android/view/View.html中关于Focus Handling的介绍,

我们知道framework会根据用户的输入处理常规的focus移动,包括当删除、隐藏或添加新的view时改变focus。一个view有资格获得

focus的前提是isFocusable()方法返回true,你可以通过setFocusable(boolean)方法来设置它。另外当在touch mode下的时候,还

需要isFocusableInTouchMode()也返回true,你也可以通过setFocusableInTouchMode(boolean)来设置它。focus的移动是基于这

样的算法,它尝试在某个给定的方向上找最临近的view,设置它为新的focus。在极个别情况,如果默认的算法不符合你的需求,你也可以

在xml布局文件中通过显式指定nextFocusDown/Left/Right/Up这些属性来表明focus移动的顺序。在运行时刻,你也可以通过调用

View.requestFocus()方法来动态地让某个view获得focus。作为开始,我们先看看这几个具备获得焦点前提的方法,如下:

    /**
     * Returns whether this View is able to take focus.
     *
     * @return True if this view can take focus, or false otherwise.
     * @attr ref android.R.styleable#View_focusable
     */
    @ViewDebug.ExportedProperty(category = "focus")
    public final boolean isFocusable() {
        return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK); // 各种位操作,不熟悉、习惯的同学可以翻本C语言的书看看,
    }                                                      // 这里顺便推荐下《C Primer Plus》,一本足矣,而且里面
                                                           // 有一章是专门介绍bit操作的应用的,非常赞!!!
    /**
     * When a view is focusable, it may not want to take focus when in touch mode.
     * For example, a button would like focus when the user is navigating via a D-pad
     * so that the user can click on it, but once the user starts touching the screen,
     * the button shouldn‘t take focus
     * @return Whether the view is focusable in touch mode.
     * @attr ref android.R.styleable#View_focusableInTouchMode
     */
    @ViewDebug.ExportedProperty
    public final boolean isFocusableInTouchMode() {
        return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
    }

    /**
     * Set whether this view can receive the focus.
     *
     * Setting this to false will also ensure that this view is not focusable
     * in touch mode.
     *
     * @param focusable If true, this view can receive the focus.
     *
     * @see #setFocusableInTouchMode(boolean)
     * @attr ref android.R.styleable#View_focusable
     */
    public void setFocusable(boolean focusable) {
        if (!focusable) { // 注意:是false的时候,会顺便保证在touch mode下也不能获得focus
            setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
        }
        setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK); // 设置FOCUSABLEB位
    }

    /**
     * Set whether this view can receive focus while in touch mode.
     *
     * Setting this to true will also ensure that this view is focusable.
     *
     * @param focusableInTouchMode If true, this view can receive the focus while
     *   in touch mode.
     *
     * @see #setFocusable(boolean)
     * @attr ref android.R.styleable#View_focusableInTouchMode
     */
    public void setFocusableInTouchMode(boolean focusableInTouchMode) {
        // Focusable in touch mode should always be set before the focusable flag
        // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
        // which, in touch mode, will not successfully request focus on this view
        // because the focusable in touch mode flag is not set
        setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
        if (focusableInTouchMode) { // 如果是true顺便打开FOCUSABLE位
            setFlags(FOCUSABLE, FOCUSABLE_MASK);
        }
    }

接下来我们就看看本文的重点View.requestFocus()等相关方法,代码如下:

/**
     * Call this to try to give focus to a specific view or to one of its
     * descendants.
     *
     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
     * false), or if it is focusable and it is not focusable in touch mode
     * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
     *
     * See also {@link #focusSearch(int)}, which is what you call to say that you
     * have focus, and you want your parent to look for the next one.
     *
     * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
     * {@link #FOCUS_DOWN} and <code>null</code>.
     *
     * @return Whether this view or one of its descendants actually took focus.
     */
    public final boolean requestFocus() {
        return requestFocus(View.FOCUS_DOWN);
    }

    /**
     * Call this to try to give focus to a specific view or to one of its
     * descendants and give it a hint about what direction focus is heading.
     *
     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
     * false), or if it is focusable and it is not focusable in touch mode
     * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
     *
     * See also {@link #focusSearch(int)}, which is what you call to say that you
     * have focus, and you want your parent to look for the next one.
     *
     * This is equivalent to calling {@link #requestFocus(int, Rect)} with
     * <code>null</code> set for the previously focused rectangle.
     *
     * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
     * @return Whether this view or one of its descendants actually took focus.
     */
    public final boolean requestFocus(int direction) {
        return requestFocus(direction, null);
    }

    /**
     * Call this to try to give focus to a specific view or to one of its descendants
     * and give it hints about the direction and a specific rectangle that the focus
     * is coming from.  The rectangle can help give larger views a finer grained hint
     * about where focus is coming from, and therefore, where to show selection, or
     * forward focus change internally.
     *
     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
     * false), or if it is focusable and it is not focusable in touch mode
     * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
     *
     * A View will not take focus if it is not visible.
     *
     * A View will not take focus if one of its parents has
     * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
     * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
     *
     * See also {@link #focusSearch(int)}, which is what you call to say that you
     * have focus, and you want your parent to look for the next one.
     *
     * You may wish to override this method if your custom {@link View} has an internal
     * {@link View} that it wishes to forward the request to.
     *
     * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
     * @param previouslyFocusedRect The rectangle (in this View‘s coordinate system)
     *        to give a finer grained hint about where focus is coming from.  May be null
     *        if there is no hint.
     * @return Whether this view or one of its descendants actually took focus.
     */
    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        return requestFocusNoSearch(direction, previouslyFocusedRect);
    }

    private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) { // 此方法就是最终被调用的版本
        // need to be focusable
        if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE || // 不是FOCUSABLE,即没资格获取焦点
                (mViewFlags & VISIBILITY_MASK) != VISIBLE) { // 或者不是VISIBLE的,都直接返回false,表示请求获取焦点失败
            return false; // 所以除了上文提到的2个获取focus的前提,其实这里的VISIBLE也应该算是第3个前提吧!
        }

        // need to be focusable in touch mode if in touch mode
        if (isInTouchMode() && // 同样在touch mode下,也要检测FOCUSABLE_IN_TOUCH_MODE标志
            (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
               return false; // 不满足也直接返回false,表示失败
        }

        // need to not have any parents blocking us
        if (hasAncestorThatBlocksDescendantFocus()) { // parents阻止我们获得焦点的话,我们也只能以失败告终
            return false;
        }
        // 以上重重关卡都通过了,才会走到这里,真正设置focus
        handleFocusGainInternal(direction, previouslyFocusedRect);
        return true;
    }

    /**
     * Give this view focus. This will cause
     * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
     *
     * Note: this does not check whether this {@link View} should get focus, it just
     * gives it focus no matter what.  It should only be called internally by framework
     * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
     *
     * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
     *        {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
     *        focus moved when requestFocus() is called. It may not always
     *        apply, in which case use the default View.FOCUS_DOWN.
     * @param previouslyFocusedRect The rectangle of the view that had focus
     *        prior in this View‘s coordinate system.
     */
    void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
        if (DBG) {
            System.out.println(this + " requestFocus()");
        }

        if ((mPrivateFlags & PFLAG_FOCUSED) == 0) { // 只有当前View不是focused view时才会发生一系列操作,否则do nothing
            mPrivateFlags |= PFLAG_FOCUSED; // 如果没focus的话,先设置此view的focused标志,isFocused,hasFocus等方法会检测此标志

            View oldFocus = (mAttachInfo != null) ? getRootView().findFocus() : null; // 找到之前的focus

            if (mParent != null) {
                mParent.requestChildFocus(this, this); // 如果有mParent,则将此新focus请求向上传递
            }

            if (mAttachInfo != null) { // callback接口,将focus change事件notify出去
                mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, this);
            }

            onFocusChanged(true, direction, previouslyFocusedRect);
            refreshDrawableState();
        }
    }

接着我们看下关于PFLAG_FOCUSED标志位相关的几个方法,如下:

/**
     * Returns true if this view has focus
     *
     * @return True if this view has focus, false otherwise.
     */
    @ViewDebug.ExportedProperty(category = "focus")
    public boolean isFocused() {
        return (mPrivateFlags & PFLAG_FOCUSED) != 0;
    }

    /**
     * Find the view in the hierarchy rooted at this view that currently has
     * focus.
     *
     * @return The view that currently has focus, or null if no focused view can
     *         be found.
     */
    public View findFocus() {
        return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
    }

    /**
     * Returns true if this view has focus iteself, or is the ancestor of the
     * view that has focus.
     *
     * @return True if this view has or contains focus, false otherwise.
     */
    @ViewDebug.ExportedProperty(category = "focus")
    public boolean hasFocus() { // 对View来说,hasFocus和isFocus是相同的,ViewGroup类重载了此方法
        return (mPrivateFlags & PFLAG_FOCUSED) != 0;
    }

  最后,我们看看ViewParent接口(以及其实现ViewGroup)的requestChildFocus()实现,代码如下:

    /**
     * Called when a child of this parent wants focus
     *
     * @param child The child of this ViewParent that wants focus. This view
     *        will contain the focused view. It is not necessarily the view that
     *        actually has focus.
     * @param focused The view that is a descendant of child that actually has
     *        focus
     */
    public void requestChildFocus(View child, View focused); // parent中的某个child请求获得focus,child要么是focused,
                                                             // 要么是focused的parent
    // 我们可以看到其实现类有ViewGroup、ScrollView等,这里我们看下ViewGroup类的,其他的有兴趣的同学可以自行研究

    @Override
    public void requestChildFocus(View child, View focused) {
        if (DBG) {
            System.out.println(this + " requestChildFocus()");
        }
        if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
            return; // 如果此ViewGroup被设置为阻止任何children获得focus,则直接返回
        }

        // Unfocus us, if necessary
        super.unFocus(); // 先unFocus 此ViewGroup

        // We had a previous notion of who had focus. Clear it.
        if (mFocused != child) { // 如果mFocused不同于传递进来的child,则更新mFocused
            if (mFocused != null) {
                mFocused.unFocus(); // 让旧的放弃focus
            }

            mFocused = child; // 更新mFocused
        }
        if (mParent != null) { // 接着沿着focus path往上传递(递归调用)
            mParent.requestChildFocus(this, focused); // 注意这里的第2个参数,一直是传递进来的focused不变
        }
    }

我们注意到只有ViewGroup才有mFocused字段,表示focus path上的一个节点。我们看看与之相关的代码:

    // The view contained within this ViewGroup that has or contains focus.
    private View mFocused; // 此ViewGroup中的child view,它要么是focused view本身要么包含focused view

    @Override
    void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { // 此方法重载了View中的
        if (mFocused != null) { // 添加了对mFocused的处理
            mFocused.unFocus(); // 让mFocused unFocus在这种情况下
            mFocused = null;
        }
        super.handleFocusGainInternal(direction, previouslyFocusedRect);
    }

    /**
     * {@inheritDoc}
     */
    public void clearChildFocus(View child) {
        if (DBG) {
            System.out.println(this + " clearChildFocus()");
        }

        mFocused = null; // 清空
        if (mParent != null) { // 将事件告诉parent
            mParent.clearChildFocus(this);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clearFocus() {
        if (DBG) {
            System.out.println(this + " clearFocus()");
        }
        if (mFocused == null) { // 如果没有mFocused,则ViewGroup自身clearFocus
            super.clearFocus();
        } else { // 否则,让mFocused clearFocus,并且重置为null
            View focused = mFocused;
            mFocused = null;
            focused.clearFocus();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    void unFocus() { // 大体同clearFocus,只是调的是unFocus方法
        if (DBG) {
            System.out.println(this + " unFocus()");
        }
        if (mFocused == null) {
            super.unFocus();
        } else {
            mFocused.unFocus();
            mFocused = null;
        }
    }

    /**
     * Returns the focused child of this view, if any. The child may have focus
     * or contain focus.
     *
     * @return the focused child or null.
     */
    public View getFocusedChild() { // 返回这个字段,供客户端代码使用
        return mFocused;
    }

    /**
     * Returns true if this view has or contains focus
     *
     * @return true if this view has or contains focus
     */
    @Override
    public boolean hasFocus() { // ViewGroup自己是focused或者其子、孙后代包含focused view
        return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.view.View#findFocus()
     */
    @Override
    public View findFocus() {
        if (DBG) {
            System.out.println("Find focus in " + this + ": flags="
                    + isFocused() + ", child=" + mFocused);
        }

        if (isFocused()) { // 自己是focused,直接返回this
            return this;
        }

        if (mFocused != null) { // 否则,mFocused不为空,则沿着这条线往下继续找
            return mFocused.findFocus();
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean hasFocusable() {
        if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
            return false;
        }

        if (isFocusable()) {
            return true;
        }

        final int descendantFocusability = getDescendantFocusability();
        if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
            final int count = mChildrenCount;
            final View[] children = mChildren;

            for (int i = 0; i < count; i++) {
                final View child = children[i];
                if (child.hasFocusable()) {
                    return true;
                }
            }
        }

        return false;
    }

接着我们看下View自己的unFocus、clearFocus实现,代码如下:

    /**
     * Called internally by the view system when a new view is getting focus.
     * This is what clears the old focus.
     * <p>
     * <b>NOTE:</b> The parent view‘s focused child must be updated manually
     * after calling this method. Otherwise, the view hierarchy may be left in
     * an inconstent state.
     */
    void unFocus() {
        if (DBG) {
            System.out.println(this + " unFocus()");
        }

        clearFocusInternal(false, false);
    }

    /**
     * Called when this view wants to give up focus. If focus is cleared
     * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
     * <p>
     * <strong>Note:</strong> When a View clears focus the framework is trying
     * to give focus to the first focusable View from the top. Hence, if this
     * View is the first from the top that can take focus, then all callbacks
     * related to clearing focus will be invoked after wich the framework will
     * give focus to this view.
     * </p>
     */
    public void clearFocus() {
        if (DBG) {
            System.out.println(this + " clearFocus()");
        }

        clearFocusInternal(true, true);
    }

    /**
     * Clears focus from the view, optionally propagating the change up through
     * the parent hierarchy and requesting that the root view place new focus.
     *
     * @param propagate whether to propagate the change up through the parent
     *            hierarchy
     * @param refocus when propagate is true, specifies whether to request the
     *            root view place new focus
     */
    void clearFocusInternal(boolean propagate, boolean refocus) {
        if ((mPrivateFlags & PFLAG_FOCUSED) != 0) { // 如果当前是focused,先清掉PFLAG_FOCUSED位
            mPrivateFlags &= ~PFLAG_FOCUSED;

            if (propagate && mParent != null) {
                mParent.clearChildFocus(this); // 如果向上传播的话,调用parent.clearChildFocus方法
            }

            onFocusChanged(false, 0, null); // 调用callback方法

            refreshDrawableState(); // 刷新drawable状态

            if (propagate && (!refocus || !rootViewRequestFocus())) {
                notifyGlobalFocusCleared(this);
            }
        }
    }

  通过上一篇的介绍,我们知道KeyEvent的派发就是在view层次结构的focus path上自上而下发生的,具体参见View.dispatchKeyEvent

的方法doc。刚开始我一直不明白这里的focus path是怎么形成的,怎么按着这个链传递的。这里为了帮助大家理解,我举一个典型的例子,

通过例子可以很清楚的看到传递过程。比方说我们的view层次结构是这样的,C是个Button,B是C的parent,LinearLayout,A是B的parent,

FrameLayout。这里我们先假设C、B、A都是有资格且其parent都不阻止它获得焦点,当我们在代码里调用C.requestFocus()时发生的调用

序列如下:

1. --> B.requestChildFocus(C, C); 当此方法发生后产生的结果是:B.mFocused = C;接着产生2调用;

2. --> A.requestChildFocus(B, C); 同样的,当此方法发生后,A.mFocused = B; 接着往上传递直到parent为空时停止。

当C.requestFocus()调用结束时,如果没有各种失败的case发生,那么C就是当前view层次结构中的focus了,也就是C.isFocused()方法

此时会返回true。看到了吗?通过这个递归调用,focus path的链就形成了,从最顶层的A能通过其mFocused字段找到B,从找到的B能通过

其mFocused字段找到C,以此类推。为了加深这个印象,我们最后再看眼ViewGroup.dispatchKeyEvent()方法:

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        /// 2.2.1.1...
        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onKeyEvent(event, 1);
        }

        if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
                == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { // ViewGroup是focused,则优先交给它自己处理
            /// 2.2.1.2.
            if (super.dispatchKeyEvent(event)) {
                return true;
            }
        } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
                == PFLAG_HAS_BOUNDS) { // 否则就沿着mFocused形成的focus path向下传递
            /// 2.2.1.3.
            if (mFocused.dispatchKeyEvent(event)) {
                return true;
            }
        }

        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
        }
        /// 2.2.1.4.
        return false;
    }

  现在再回过头来看这里的逻辑,是不是感觉特别简单呢?那是因为你已经完全弄清楚了mFocused的由来以及各种变化过程。至此view层次

结构中关于focus的变化过程已经全部分析完毕了,enjoy。

View Focus的处理过程及ViewGroup的mFocused字段分析

时间: 2024-10-05 21:40:17

View Focus的处理过程及ViewGroup的mFocused字段分析的相关文章

Android View框架总结(八)ViewGroup事件分发机制

请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52298780 上篇分析了View的事件分发流程,留了一个问题:如果上面的EventButton继承TextView的话,按下抬起,会有一个现象,我可以告诉大家现象:就是只有dispatchTouchEvent ACTION_DOWN,onTouch ACTION_DOWN,onTouchEvent ACTION_DOWN这三个,你移动,或者抬起,是没有MOVE,或者

源码解析Android中View的measure量算过程

Android中的Veiw从内存中到呈现在UI界面上需要依次经历三个阶段:量算 -> 布局 -> 绘图,关于View的量算.布局.绘图的总体机制可参见博文< Android中View的布局及绘图机制>.量算是布局和绘图的基础,所以量算是很重要的一个环节.本文将从源码角度解析View的量算过程,这其中会涉及某些关键类以及关键方法. 对View进行量算的目的是让View的父控件知道View想要多大的尺寸. 量算过程概述 如果要进行量算的View是ViewGroup类型,那么ViewGr

47.Android View的加载过程 (转)

原文地址:http://blog.csdn.net/xyz_lmn/article/details/20122303 大家都知道Android中加载view是从Activity的onCreate方法调用setContentView开始的,那么View的具体加载过程又是怎么的呢?这一节我们做一下分析. 首先追踪一下代码: Activity中: [java] view plain copy print? public void setContentView(int layoutResID) { ge

源码解析Android中View的layout布局过程

Android中的Veiw从内存中到呈现在UI界面上需要依次经历三个阶段:量算 -> 布局 -> 绘图,关于View的量算.布局.绘图的总体机制可参见博文 < Android中View的布局及绘图机制>.量算是布局的基础,如果想了解量算的细节,可参见博文<源码解析Android中View的measure量算过程>.本文将从源码角度解析View的布局layout过程,本文会详细介绍View布局过程中的关键方法,并对源码加上了注释以进行说明. 对View进行布局的目的是计算

【Android 1.6】View和ViewGroup的touch事件分析和总结

ENV: android 1.6 目前Android版本已经到了7.0(nougat)了,Android 随着版本升级,touch事件的源码也在跟随着系统的升级而写得越来越复杂,加入了很多旁枝末节,这些旁枝末节,对于分析流程是一种干扰:由于Android的版本升级是向下兼容的,万变不离其宗,研究Android早期的版本,可以更容易理解touch事件的分发,本篇以Android1.6版本的源码进行讲解,由简及繁,理解了早期的源码,再进入高版本的研究也会更容易许多. 前言: View事件的派发其实非

控制器View的加载过程

1.控制器内部的view是延迟加载 1> 用到时再加载2> 加载完毕后会调用控制器的viewDidLoad方法 2.创建控制器的方式 1> 直接通过代码创建OneViewController *one = [[OneViewController alloc] init]; 如果调用init方法创建控制器(TwoViewController),那么在加载控制器view的时候: a.优先查找 “TwoView.xib” b.其次再查找 同名 的xib 文件 “TwoViewControlle

Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析

在Android开发中为了inflate一个布局文件,大体有2种方式,如下所示: // 1. get a instance of LayoutInflater, then do whatever you want LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 2. you're in some View class, then jus

Android View体系(十一)自定义ViewGroup

相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源码解析Scroller Android View体系(五)从源码解析View的事件分发机制 Android View体系(六)从源码解析Activity的构成 Android View体系(七)从源码解析View的measure流程 Android View体系(八)从源码解析View的layout

[Android FrameWork 6.0源码学习] View的重绘过程

View绘制的三部曲,  测量,布局,绘画今天我们分析测量过程 view的测量是从ViewRootImpl发起的,View需要重绘,都是发送请求给ViewRootImpl,然后他组织重绘在重绘的过程中,有一步就是测量,通过代码来分析测量过程 private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp, final Resources res, final int desiredWind