如何在Android中绘制形状
问题描述:
我正在学习Android,并尝试在画布上绘制不同的形状。目前,我坚持用不同的角度椭圆:如何在Android中绘制形状
我试着使用path.addRoundRect()
方法(其中一个需要半径的阵列),但无法弄清楚怎样设定我通过那里以达到这样的形状。我也尝试过使用path.lineTo()
,但无法达到这样的效果(这有点类似,但仍然不是我所需要的)。什么会是一个很好的解决方案来完成这个?
编辑1:我曾尝试为以下几点:
Path path= new Path();
path.moveTo(x - radius, y - radius/ 1.5f);
path.lineTo(x - radius/ 4, y - radius);
path.lineTo(x, y - radius);
path.lineTo(x + radius/ 2, y - radius);
path.lineTo(x + radius, y - radius/ 2);
path.lineTo(x, y + radius/ 2);
path.lineTo(x - radius/ 2, y + radius/ 1.5f);
path.lineTo(x - radius, y + radius/ 4);
path.lineTo(x - radius, y - radius/ 1.5f);
path.close();
Paint pathPaint = new Paint();
pathPaint.setColor(Color.BLACK);
pathPaint.setStrokeWidth(2.5f);
pathPaint.setDither(true);
pathPaint.setStyle(Style.STROKE);
pathPaint.setStrokeJoin(Join.ROUND);
pathPaint.setStrokeCap(Cap.ROUND);
pathPaint.setPathEffect(new CornerPathEffect(20));
pathPaint.setAntiAlias(true);
canvas.drawOval(new RectF(x - radius, y - radius+ 2, x + radius-2, y + radius- 2), pathPaint);
canvas.drawPath(path, pathPaint);
X和Y是显示器和半径上的一些坐标是圆的半径(我开始用圆圈绘制)。它等于14 px。
我也试着这样说:
float[] radii = new float[] {
5,
5,
1,
1,
5,
1,
1,
1,
};
path.addRoundRect(new RectF(x - radius, y - radius, x + radius,
y + radius),
radii, Direction.CW);
canvas.drawPath(path, pathPaint);
答
尝试Canvas.skew()函数。下面的URL有一个很好的例子。倾斜“倾斜”正在绘制的点。根据画布的当前中心,您将得到不同的结果,因此可以根据需要使用translate()来获得所需的结果。
+0
谢谢你的建议。我无法验证它,因为我不在这个项目atm上工作。也许其他人可以证实,如果它工作与否。 – 2016-05-17 10:14:00
你可以发布你的代码吗? – petey 2013-04-26 17:53:58
http://developer.android.com/reference/android/graphics/drawable/shapes/OvalShape.html – 2013-04-26 17:55:18