假设一个控件使用Drawable作为背景,那么如果你想通过setBounds方法来控制背景的显示区域,那么你达不到目的.
原因是 当drawable被设置成控件背景后,当这个控件被绘制时(也就是draw(canvas)被调用时),控件在绘制背景时会自动更改其bounds为控件大小.
所以无论你怎么设置,系统都会在绘制这个控件背景时将其Bounds改为控件大小.
附上源码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27public void draw(Canvas canvas) { final int privateFlags = mPrivateFlags; final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE && (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState); 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 * 3. Draw view's content * 4. Draw children * 5. If necessary, draw the fading edges and restore layers * 6. Draw decorations (scrollbars for instance) */ // Step 1, draw the background, if needed int saveCount; if (!dirtyOpaque) { <strong>drawBackground(canvas);</strong> }....
复制代码
1
2
3
4
5
6
7
8
9
10
11private void drawBackground(Canvas canvas) { final Drawable background = mBackground; if (background == null) { return; } if (mBackgroundSizeChanged) { <strong>background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);</strong> mBackgroundSizeChanged = false; mPrivateFlags3 |= PFLAG3_OUTLINE_INVALID; }....
最后
以上就是温暖小蝴蝶最近收集整理的关于不要使用Drawable的setBounds方法来指定其在控件作为背景的显示位置的全部内容,更多相关不要使用Drawable内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复