您的位置:首页 > 其它

不要使用Drawable的setBounds方法来指定其在控件作为背景的显示位置

2015-03-05 00:32 357 查看
假设一个控件使用Drawable作为背景,那么如果你想通过setBounds方法来控制背景的显示区域,那么你达不到目的.

原因是 当drawable被设置成控件背景后,当这个控件被绘制时(也就是draw(canvas)被调用时),控件在绘制背景时会自动更改其bounds为控件大小.

所以无论你怎么设置,系统都会在绘制这个控件背景时将其Bounds改为控件大小.

附上源码

public 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>
}....

private 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
相关文章推荐