android OOM的那些事
View.java
1. /**
2.
3. * Simple constructor to use when creating a view from code.
4.
5. *
6.
7. * @param context The Context the view is running in, through which it can
8.
9. * access the current theme, resources, etc.
10.
11. */
12.
13. public View(Context context) {
14.
15. mContext = context;
16.
17. mResources = context != null ? context.getResources() : null;
18.
19. mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
20.
21. // Used for debug only
22.
23. //++sInstanceCount;
24.
25. mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
26.
27. }
View 保存了对context的引用,mContext = context;
1.我们知道结构其实和DOM差不多,都会保存其父容器、子容器的引用,因而context的使用需要注意,不要使用静态的子View。
2.来看View中的setBackgroundDrawable方法
View.java
1. /**
2.
3. * Set the background to a given Drawable, or remove the background. If the
4.
5. * background has padding, this View's padding is set to the background's
6.
7. * padding. However, when a background is removed, this View's padding isn't
8.
9. * touched. If setting the padding is desired, please use
10.
11. * {@link #setPadding(int, int, int, int)}.
12.
13. *
14.
15. * @param d The Drawable to use as the background, or null to remove the
16.
17. * background
18.
19. */
20.
21. public void setBackgroundDrawable(Drawable d) {
22.
23. boolean requestLayout = false;
24.
25.
26.
27. mBackgroundResource = 0;
28.
29.
30.
31. /*
32.
33. * Regardless of whether we're setting a new background or not, we want
34.
35. * to clear the previous drawable.
36.
37. */
38.
39. if (mBGDrawable != null) {
40.
41. mBGDrawable.setCallback(null);
42.
43. unscheduleDrawable(mBGDrawable);
44.
45. }
46.
47.
48.
49. if (d != null) {
50.
51. Rect padding = sThreadLocal.get();
52.
53. if (padding == null) {
54.
55. padding = new Rect();
56.
57. sThreadLocal.set(padding);
58.
59. }
60.
61. if (d.getPadding(padding)) {
62.
63. setPadding(padding.left, padding.top, padding.right, padding.bottom);
64.
65. }
66.
67.
68.
69. // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
70.
71. // if it has a different minimum size, we should layout again
72.
73. if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
74.
75. mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
76.
77. requestLayout = true;
78.
79. }
80.
81.
82.
83. d.setCallback(this);
84.
85. if (d.isStateful()) {
86.
87. d.setState(getDrawableState());
88.
89. }
90.
91. d.setVisible(getVisibility() == VISIBLE, false);
92.
93. mBGDrawable = d;
94.
95.
96.
97. if ((mPrivateFlags & SKIP_DRAW) != 0) {
98.
99. mPrivateFlags &= ~SKIP_DRAW;
100.
101. mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
102.
103. requestLayout = true;
104.
105. }
106.
107. } else {
108.
109. /* Remove the background */
110.
111. mBGDrawable = null;
112.
113.
114.
115. &nb
补充:移动开发 , Android ,