【发布时间】:2013-01-25 16:28:38
【问题描述】:
我正在尝试绘制一条弧线以覆盖在现有圆的某些部分之上。我的两个圆圈都画得很好,但我的 drawArc() 调用或 drawRect() 调用似乎都没有做任何事情。该应用程序不会崩溃,也不例外。它只是默默地失败了。
onDraw() 代码:
@Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
int size = (width > height) ? height : width;
float cx = width / 2;
float cy = height / 2;
float radius = size / 2;
float left = cx - radius;
float right = cx + radius;
float top = cy - radius;
float bottom = cy + radius;
RectF rect = new RectF(left, top, right, bottom);
RectF rect2 = new RectF(canvas.getClipBounds());
Log.d("MyTag", "Left: " + rect.left + "Right: " + rect.right + "Top: " + rect.top + "Bottom: " + rect.bottom);
Log.d("MyTag", "Left: " + rect2.left + "Right: " + rect2.right + "Top: " + rect2.top + "Bottom: "
+ rect2.bottom);
canvas.drawCircle(cx, cy, radius, circleRing);//Works
canvas.drawCircle(cx, cy, radius - barWidth, innerColor);//Works
canvas.drawArc(rect, 0, angle, true, circleColor);//Doesn't work
canvas.drawRect(rect, circleColor);//Doesn't work
super.onDraw(canvas);
}
我已确认我的circleColor 画图设置正确,并且angle 是弧线的有效值。
我的绘画在 {} 块中设置如下,以便所有构造函数都使用它:
{
circleColor = new Paint();
innerColor = new Paint();
circleRing = new Paint();
circleColor.setColor(color.holo_blue_light);
innerColor.setColor(Color.BLACK);
circleRing.setColor(Color.GRAY);
circleColor.setAntiAlias(true);
innerColor.setAntiAlias(true);
circleRing.setAntiAlias(true);
circleColor.setStrokeWidth(50);
innerColor.setStrokeWidth(5);
circleRing.setStrokeWidth(5);
circleColor.setStyle(Paint.Style.FILL);
}
我尝试过的:
- 硬编码坐标
- 使用圆圈使用的颜料
- 仅存在 drawArc() 调用并注释掉其余的绘图
- 禁用硬件加速
Logcat 显示我的 RectF 有有效点,只是顶部和底部缩放成一个正方形:
01-25 13:33:39.877: D/MyTag(21612): Left: 0.0 Right: 720.0 Top: 159.0 Bottom: 879.0 //Mine
01-25 13:33:39.877: D/MyTag(21612): Left: 0.0 Right: 720.0 Top: 0.0 Bottom: 1038.0 //Canvas'
有人知道是什么原因造成的吗?
【问题讨论】:
-
o/ 你覆盖什么视图?最后一个 drawRect 应该做什么?那不就是把所有东西都涂上吗?我刚刚尝试了一些通用的 Paint 对象和颜色,并且使用前两个 drawCircle 和 drawArc 调用似乎在我的 Galaxy Nexus 上按预期工作。
-
@kcoppock 嘿嘿:P。我添加的最后一个 drawRect 仅用于调试目的。我仍然看到我的两个圆圈,没有矩形或弧线。我直接扩展了基础 android.view.View 类。我也在 Galaxy Nexus 上,但 draw arc 调用对我来说很简单。
-
也在 Nexus 7 上试过。同样缺少弧线。
-
嗯。只是好奇,您的画图是如何设置的?这是我使用的确切代码:pastie.org/5853922
-
@kcoppock 用颜料编辑。我也找到了解决方案。现在添加答案。
标签: android android-canvas ondraw