不要使用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被设置成控件背景后,当这个控件被绘制时(也就是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;
}....
相关文章推荐
- Delpi在MDI窗口中显示图形控件及指定背景的解决方案
- GROUP BY的另种使用方法:将结果集一列的内容作为一个单元格显示
- 使用DelphiX显示图片并把图片作为背景
- 日历控件Calendar使用技巧--根据数据库数据控制日期背景显示颜色
- Delpi在MDI窗口中显示图形控件及指定背景的解决方案
- 解决使用MasterPage后,Page.FindControl方法找不到指定控件的问题
- MFC中List Control控件的使用及实时显示系统时间的方法
- <转载>C#指定窗口显示位置的方法
- as3:sprite作为容器使用时,最好不要指定width,height
- 《VC中自动改变控件位置和大小的对话框类》详细使用方法
- [网络收集]解决在母版页中使用Page.FindControl方法找不到指定控件的问题
- iphone(objective-c)相当于j2me的drawRegion方法 截取部分图片显示到指定位置
- 使用ASPOSE.WORD组件,在word文档指定的位置动态生成表格,支持单元格合并及背景填充
- VC使用位图作为对话框或者控件背景
- 使用addView方法时,如何保持已有动态控件位置不被改变
- C#/winform 指定窗口显示位置的方法
- AJAX 的DragPanelExtender 拖曳控件使用,并保存位置(此方法思路可将坐标保存到数据库)
- 记录:树形控件的一些使用方法(和sql server 结合显示)
- 在Silverlight页面里显示HTML的免费控件下载(附使用方法代码)
- 在Silverlight页面里显示HTML的免费控件下载(附使用方法代码)