我正在尝试绘制圆弧以叠加在现有圆的某个部分的顶部。我的两个圆圈画得很好,但是我的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);
}
我尝试过:
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'
有人知道造成这种情况的原因吗?
答案 0 :(得分:1)
原来问题就在于行
circleColor.setColor(color.holo_blue_light);
虽然Android SDK将其定义为:
浅蓝色的Holo色调
常数值:17170450(0x01060012)
就我的Canvas而言,它似乎不是一个有效的颜色(请注意,这是在API 14中添加的,我正在Android 4.2上进行测试,所以我应该可以使用它)。但是,将其更改为使用更多...正常颜色可以正常工作:
circleColor.setColor(Color.GREEN);