我正在将 Drawable 绘制到 android 中的自定义 View 。
当显示自定义 View 时,父级是 ScrollView。当 ScrollView 移动时,Drawable 会缩小直到离开屏幕,然后在您滚动回自定义小部件时变大,即使它明确设置为 219dip 正方形。
原因似乎是我在 onDraw(Canvas canvas) 方法中获取边界的方式
int mRight;
int mLeft;
int mBottom;
int mTop;
mRight = canvas.getClipBounds().right;
mLeft = canvas.getClipBounds().left;
mBottom = canvas.getClipBounds().bottom;
mTop = canvas.getClipBounds().top;
int availableWidth = mRight - mLeft;
int availableHeight = mBottom - mTop;
ClipBound 似乎会发生变化以反射(reflect)屏幕上有多少 View 。因此,调整 Drawables 的大小,直到它变得非常小并离开屏幕。
我试着用这个替换上面的代码
int availableWidth = canvas.getWidth();
int availableHeight = canvas.getHeight();
现在屏幕上没有绘制任何东西?这不应该工作吗?
最佳答案
在绘制我的自定义 Drawable 时,我使用:
float w = this.getBounds().width();
float h = this.getBounds().height();
在draw(Canvas canvas)
方法中获取我的Drawable的边界区域。我没有在 ScrollView 中尝试过这个,但我认为 Drawable 的边界应该以任何一种方式工作。
希望这对您有所帮助。
关于android - 在android中的 Canvas 上绘制时获取边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5399437/