如何在 onDraw 方法之外获取自定义视图的画布大小?
我需要能够访问视图画布的大小来执行一些计算。由于某种原因,传递给 onSizeChanged
的视图大小与传递给 onDraw
的画布大小不同。我当前的解决方法使用布尔标志来确定何时需要进行计算。
理想的解决方案将允许我在 onSizeChanged
方法中进行这些计算,所以我想知道......有什么方法可以获取 Canvas
对象(或者至少是尺寸)在 onDraw 方法之外?
我的代码如下。它以给定角度绘制圆的半径。当我使用canvas.centerX()来确定半径的起点和终点时,一切都很完美。如果我使用传递给 onSizeChanged
的参数,它甚至与正确的结果相差甚远。
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mSizeChanged = true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mSizeChanged) {
RectF bounds = new RectF(canvas.getClipBounds());
float centerX = bounds.centerX();
float centerY = bounds.centerY();
float radianAngle = (float) Math.toRadians(mStartAngle);
mRadius[0] = center;
mRadius[1] = center;
mRadius[2] = center + center * FloatMath.cos(radianAngle);
mRadius[3] = center + center * FloatMath.sin(radianAngle);
mSizeChanged = false;
}
mPaint.setColor(0xFF330000);
mPaint.setStrokeWidth(1);
canvas.drawLines(mRadius, mPaint);
}
I need to be able to access the size of the view's canvas to perform some calculations. For some reason, the size of the view passed to onSizeChanged
is different than the size of the canvas passed to onDraw
. My current workaround uses a boolean flag to determine when I need to do the calculations.
The ideal solution would allow me to do these calculations in the onSizeChanged
method, so I'm wondering... is there any way I can get ahold of the Canvas
object (or at least it's dimensions) outside of the onDraw
method?
My code is below. It is draws the radius of a circle at a given angle. When I use canvas.centerX()
to determine the start points and end points for the radius, everything works perfectly. If I use the parameters passed into onSizeChanged
, it isn't even remotely close to correct.
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mSizeChanged = true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mSizeChanged) {
RectF bounds = new RectF(canvas.getClipBounds());
float centerX = bounds.centerX();
float centerY = bounds.centerY();
float radianAngle = (float) Math.toRadians(mStartAngle);
mRadius[0] = center;
mRadius[1] = center;
mRadius[2] = center + center * FloatMath.cos(radianAngle);
mRadius[3] = center + center * FloatMath.sin(radianAngle);
mSizeChanged = false;
}
mPaint.setColor(0xFF330000);
mPaint.setStrokeWidth(1);
canvas.drawLines(mRadius, mPaint);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出于绘图目的,您不应该真正使用
Canvas
对象的尺寸。只需使用
onSizeChanged
方法中提供给您的尺寸即可。您可以存储尺寸以供onDraw
方法使用,也可以调整大小/绘制到背景位图以供稍后绘制。更新:
快速编写了一些代码,看起来像这样:
更新2
继圈子代码更新之后。
我们可以这样做:
您将看到这现在适用于任何大小的视图。
For drawing purposes, you should not really use the dimensions of the
Canvas
object.Just use the dimensions provided to you in the
onSizeChanged
method. You can either store the dimensions for use in theonDraw
method or resize/draw to a backing bitmap that you can draw with later.Update:
Quickly whipped up some code, it looks like this works:
Update 2
Following the circle code update.
We can do this:
You'll see that this now works on any sized view.