如何绘制非方形刻度线

时间:2017-08-29 19:47:47

标签: java android wear-os

在下面的图片中,我试图弄清楚如何绘制表盘上看到的刻度线。我知道如何绘制线条,但我不确定如何绘制像图片那样的“锥形”线条。如何为Android Wear完成此操作?我想我需要创建Path,但我不知道如何生成Path坐标。

ticks

1 个答案:

答案 0 :(得分:2)

要使用Path,您需要执行以下操作:

Path path = new Path();

// Start at the top left corner
path.moveTo(screenCenterX - halfMarkerTopWidth, topMargin);

// Draw a line to the top right corner
path.lineTo(screenCenterX + halfMarkerTopWidth, topMargin);

// Draw a line to the bottom right corner
path.lineTo(screenCenterX + halfMarkerBottomWidth, topMargin + markerHeight);

// Draw a line to the bottom left corner
path.lineTo(screenCenterX - halfMarkerBottomWidth, topMargin + markerHeight);

// Close the Path (will automatically draw a line back to the top left corner)
path.close();

然后绘制您的路径12次,并在每次绘制时将画布旋转30度。

canvas.save();
for (int i = 0; i < 12; i++) {
    canvas.rotate(30, screenCenterX, screenCenterY);
    canvas.drawPath(path, paint);
}
canvas.restore();