展会信息港展会大全

Android-View 的刷新机制
来源:互联网   发布日期:2016-01-14 09:50:51   浏览:3219次  

导读:在Android的布局体系中,父View负责刷新、布局显示子View;而当子View需要刷新时,则是通知父View来完成。这种处理逻辑在View的代码中明确的表现出来:01publicvoidinvalidate() {02finalViewParent p = mPa......

在Android的布局体系中,父View负责刷新、布局显示子View;而当子View需要刷新时,则是通知父View来完成。这种处理逻辑在View的代码中明确的表现出来:

01

public void invalidate() {

02

final ViewParent p = mParent;

03

final AttachInfo ai = mAttachInfo;

04

if (p != null && ai != null) {

05

final Rect r = ai.mTmpInvalRect;

06

07

// 设置刷新区域为自己的尺寸

08

r.set(0, 0, mRight - mLeft, mBottom - mTop);

09

p.invalidateChild(this, r);

10

}

11

}

子View调用invalidate时,首先找到自己父View(View的成员变量mParent记录自己的父View),然后将AttachInfo中保存的信息告诉父View刷新自己。

View的父子关系的建立分为两种情况:

1) View加入ViewGroup中

01

private void addViewInner(View child, int index, LayoutParams params, booleanpreventRequestLayout) {

02

.....

03

// tell our children

04

if (preventRequestLayout) {

05

child.assignParent(this);

06

} else {

07

child.mParent = this;

08

}

09

.....

10

}

11

12

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView){

13

.....

14

view.assignParent(this);

15

....

16

}

AttachInfo是在View第一次attach到Window时,ViewRoot传给自己的子View的。这个AttachInfo之后,会顺着布局体系一直传递到最底层的View。

View.java

1

void dispatchAttachedToWindow(AttachInfo info, int visibility) {

2

mAttachInfo = info;

3

4

.....

5

}

ViewGroup.java

1

void dispatchAttachedToWindow(AttachInfo info, int visibility) {

2

super.dispatchAttachedToWindow(info, visibility);

3

4

for (int i = 0; i < count; i++) {

5

children[i].dispatchAttachedToWindow(info, visibility);

6

}

7

}

并且在新的View被加入ViewGroup时,也会将该AttachInfo传给加入的View

ViewGroup.java

1

private void addViewInner(View child, int index, LayoutParams params, booleanpreventRequestLayout) {

2

3

child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));

4

5

}

到这里明白了mParent与AttachInfo代表的意义,可以继续刷新过程的分析。

在invalidate中,调用父View的invalidateChild,这是一个从第向上回溯的过程,每一层的父View都将自己的显示区域与传入的刷新Rect做交集。

01

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

02

ViewParent parent = this;

03

04

final AttachInfo attachInfo = mAttachInfo;

05

if (attachInfo != null) {

06

final int[] location = attachInfo.mInvalidateChildLocation;

07

08

// 需要刷新的子View的位置

09

location[CHILD_LEFT_INDEX] = child.mLeft;

10

location[CHILD_TOP_INDEX] = child.mTop;

11

12

// If the child is drawing an animation, we want to copy this flag onto

13

// ourselves and the parent to make sure the invalidate request goes through

14

final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;

15

16

// Check whether the child that requests the invalidate is fully opaque

17

final boolean isOpaque = child.isOpaque() && !drawAnimation && child.getAnimation() != null;

18

19

// Mark the child as dirty, using the appropriate flag

20

// Make sure we do not set both flags at the same time

21

final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;

22

23

do {

24

View view = null;

25

if (parent instanceof View) {

26

view = (View) parent;

27

}

28

29

if (drawAnimation) {

30

if (view != null) {

31

view.mPrivateFlags |= DRAW_ANIMATION;

32

} else if (parent instanceof ViewRoot) {

33

((ViewRoot) parent).mIsAnimating = true;

34

}

35

}

36

37

// If the parent is dirty opaque or not dirty, mark it dirty with the opaque

38

// flag coming from the child that initiated the invalidate

39

if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {

40

view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;

41

}

42

43

parent = parent.invalidateChildInParent(location, dirty);

44

} while (parent != null);

45

}

46

}

47

48

49

50

public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {

51

if ((mPrivateFlags & DRAWN) == DRAWN) {

52

if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=

53

FLAG_OPTIMIZE_INVALIDATE) {

54

55

// 根据父View的位置,偏移刷新区域

56

dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, location[CHILD_TOP_INDEX] - mScrollY);

57

58

final int left = mLeft;

59

final int top = mTop;

60

61

//计算实际可刷新区域

62

if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||

63

(mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {

64

mPrivateFlags &= ~DRAWING_CACHE_VALID;

65

66

location[CHILD_LEFT_INDEX] = left;

67

location[CHILD_TOP_INDEX] = top;

68

return mParent;

69

}

70

} else {

71

mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;

72

73

location[CHILD_LEFT_INDEX] = mLeft;

74

location[CHILD_TOP_INDEX] = mTop;

75

76

dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],

77

mBottom - location[CHILD_TOP_INDEX]);

78

79

return mParent;

80

}

81

}

82

83

return null;

84

}

这个向上回溯的过程直到ViewRoot那里结束,由ViewRoot对这个最终的刷新区域做刷新。

ViewRoot.java

1

public void invalidateChild(View child, Rect dirty) {

2

3

scheduleTraversals();

4

5

}

赞助本站

人工智能实验室

相关热词: View 的刷新

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港