欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android View刷新機制實例分析

 更新時間:2016年02月23日 10:34:29   作者:炫_愛羊  
這篇文章主要介紹了Android View刷新機制,結(jié)合實例形式較為詳細的分析了Android的View刷新機制功能、原理與具體使用技巧,需要的朋友可以參考下

本文實例講述了Android View刷新機制。分享給大家供大家參考,具體如下:

一、總體說明

在Android的布局體系中,父View負責刷新、布局顯示子View;而當子View需要刷新時,則是通知父View來完成。

二、代碼分析

1).ViewGroup的addView方法,理解參數(shù)的意義和傳遞

invalidate調(diào)用父類View的方法
addViewInner方法主要做的事情是

view的dispatchAttachedToWindow(AttachInfo info, int visibility)方法

1).View的invalidate方法,這是一個從下第向上回溯的過程,每一層的父View都將自己的顯示區(qū)域與傳入的刷新

Rect做交集。

void invalidate(boolean invalidateCache) {
    if (ViewDebug.TRACE_HIERARCHY) {
      ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
    }
    if (skipInvalidate()) {
      return;
    }
    if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
        (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
        (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
      mLastIsOpaque = isOpaque();
      mPrivateFlags &= ~DRAWN;
      mPrivateFlags |= DIRTY;
      if (invalidateCache) {
        mPrivateFlags |= INVALIDATED;
        mPrivateFlags &= ~DRAWING_CACHE_VALID;
      }
      final AttachInfo ai = mAttachInfo;
      final ViewParent p = mParent;
      //noinspection PointlessBooleanExpression,ConstantConditions
      if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
        if (p != null && ai != null && ai.mHardwareAccelerated) {
          // fast-track for GL-enabled applications; just invalidate the whole hierarchy
          // with a null dirty rect, which tells the ViewAncestor to redraw everything
          p.invalidateChild(this, null);
          return;
        }
      }
      if (p != null && ai != null) {
        final Rect r = ai.mTmpInvalRect;
        r.set(0, 0, mRight - mLeft, mBottom - mTop);
        // Don't call invalidate -- we don't want to internally scroll
        // our own bounds
        p.invalidateChild(this, r);//調(diào)用子類的方法完成
      }
    }
  }

2)ViewGrop的invalidateChild方法

public final void invalidateChild(View child, final Rect dirty) {
  ViewParent parent = this;
  final AttachInfo attachInfo = mAttachInfo;
  if (attachInfo != null) {
    final int[] location = attachInfo.mInvalidateChildLocation;
    // 需要刷新的子View的位置 
    location[CHILD_LEFT_INDEX] = child.mLeft;
    location[CHILD_TOP_INDEX] = child.mTop;
    // If the child is drawing an animation, we want to copy this flag onto
    // ourselves and the parent to make sure the invalidate request goes through
    final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
    // Check whether the child that requests the invalidate is fully opaque
    final boolean isOpaque = child.isOpaque() && !drawAnimation && child.getAnimation() != null;
    // Mark the child as dirty, using the appropriate flag
    // Make sure we do not set both flags at the same time
    final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
    do {
      View view = null;
      if (parent instanceof View) {
        view = (View) parent;
      }
      if (drawAnimation) {
        if (view != null) {
            view.mPrivateFlags |= DRAW_ANIMATION;
        } else if (parent instanceof ViewRoot) {
            ((ViewRoot) parent).mIsAnimating = true;
        }
      }
        // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
        // flag coming from the child that initiated the invalidate
      if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
        view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
      }
      parent = parent.invalidateChildInParent(location, dirty);
    } while (parent != null);
  }
}
public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
  if ((mPrivateFlags & DRAWN) == DRAWN) {
    if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
            FLAG_OPTIMIZE_INVALIDATE) {
      // 根據(jù)父View的位置,偏移刷新區(qū)域 
      dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, location[CHILD_TOP_INDEX] - mScrollY);
      final int left = mLeft;
      final int top = mTop;
      //計算實際可刷新區(qū)域 
      if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
            (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
        mPrivateFlags &= ~DRAWING_CACHE_VALID;
        location[CHILD_LEFT_INDEX] = left;
        location[CHILD_TOP_INDEX] = top;
        return mParent;
      }
    } else {
      mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
      location[CHILD_LEFT_INDEX] = mLeft;
      location[CHILD_TOP_INDEX] = mTop;
      dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
            mBottom - location[CHILD_TOP_INDEX]);
        return mParent;
      }
    }
    return null;
}

這個向上回溯的過程直到ViewRoot那里結(jié)束,由ViewRoot對這個最終的刷新區(qū)域做刷新

ViewRoot.java

public void invalidateChild(View child, Rect dirty) {
}

由ViewRoot對象的performTraversals()方法調(diào)用draw()方法發(fā)起繪制該View樹,值得注意的是每次發(fā)起繪圖時,并不會重新繪制每個View樹的視圖,而只會重新繪制那些“需要重繪”的視圖,View類內(nèi)部變量包含了一個標志位DRAWN,當該視圖需要重繪時,就會為該View添加該標志位。

調(diào)用流程

mView.draw()開始繪制,draw()方法實現(xiàn)的功能如下:

1 、繪制該View的背景
2 、為顯示漸變框做一些準備操作(見5,大多數(shù)情況下,不需要改漸變框)
3、調(diào)用onDraw()方法繪制視圖本身   (每個View都需要重載該方法,ViewGroup不需要實現(xiàn)該方法)
4、調(diào)用dispatchDraw ()方法繪制子視圖(如果該View類型不為ViewGroup,即不包含子視圖,不需要重載該
方法)值得說明的是,ViewGroup類已經(jīng)為我們重寫了dispatchDraw ()的功能實現(xiàn),應(yīng)用程序一般不需要重寫該
方法,但可以重載父類函數(shù)實現(xiàn)具體的功能。

4.1 dispatchDraw()方法內(nèi)部會遍歷每個子視圖,調(diào)用drawChild()去重新回調(diào)每個子視圖的draw()方法(注意,這個 地方“需要重繪”的視圖才會調(diào)用draw()方法)。值得說明的是,ViewGroup類已經(jīng)為我們重寫了dispatch

Draw()的功能實現(xiàn),應(yīng)用程序一般不需要重寫該方法,但可以重載父類函數(shù)實現(xiàn)具體的功能。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • RadioGroup實現(xiàn)單選框的多行排列

    RadioGroup實現(xiàn)單選框的多行排列

    這篇文章主要為大家詳細介紹了RadioGroup實現(xiàn)單選框的多行排列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android 自定義圓形帶刻度漸變色的進度條樣式實例代碼

    Android 自定義圓形帶刻度漸變色的進度條樣式實例代碼

    這篇文章主要介紹了Android 自定義圓形帶刻度漸變色的進度條的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • Android中NavigationView的使用與相關(guān)問題解決

    Android中NavigationView的使用與相關(guān)問題解決

    大家都知道NavigationView的引入讓 Android側(cè)邊欄實現(xiàn)起來相當方便,最近公司項目中也使用這個新的控件完成了側(cè)邊欄的改版。在使用過程中遇到一些問題所以記錄一下。本文分為兩個部分,一是基本使用,二是相關(guān)問題的解決,感興趣的朋友們下面來一起看看吧。
    2016-10-10
  • Android編程之四種Activity加載模式分析

    Android編程之四種Activity加載模式分析

    這篇文章主要介紹了Android編程之四種Activity加載模式,簡要分析了Android編程中涉及的Activity的四種加載模式,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-01-01
  • Android App中實現(xiàn)簡單的刮刮卡抽獎效果的實例詳解

    Android App中實現(xiàn)簡單的刮刮卡抽獎效果的實例詳解

    這篇文章主要介紹了Android App中實現(xiàn)簡單的刮刮卡抽獎效果的實例詳解,文中主要借助Bitmap的canvas.drawPath的api來實現(xiàn),需要的朋友可以參考下
    2016-03-03
  • Android動態(tài)時鐘壁紙開發(fā)

    Android動態(tài)時鐘壁紙開發(fā)

    這篇文章主要為大家詳細介紹了Android動態(tài)時鐘壁紙開發(fā)的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • flutter中的資源和圖片加載示例詳解

    flutter中的資源和圖片加載示例詳解

    這篇文章主要為大家介紹了flutter中的資源和圖片加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android EditText 監(jiān)聽用戶輸入完成的實例

    Android EditText 監(jiān)聽用戶輸入完成的實例

    下面小編就為大家分享一篇Android EditText 監(jiān)聽用戶輸入完成的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Android6.0獲取動態(tài)權(quán)限代碼示例

    Android6.0獲取動態(tài)權(quán)限代碼示例

    這篇文章主要介紹了Android6.0以上獲取動態(tài)權(quán)限代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • Android狀態(tài)欄微技巧(推薦)

    Android狀態(tài)欄微技巧(推薦)

    這篇文章主要介紹了Android狀態(tài)欄微技巧的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友一起學習吧
    2016-12-12

最新評論