我有扩展View类的MyView类。MyView应绘制实心三角形。我画了一个三角形,但是填不进去。这是我的onDraw()方法:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.BLACK);
canvas.drawPaint(paint);
paint.setStrokeWidth(4);
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
Point a = new Point(0, 0);
Point b = new Point(0, 100);
Point c = new Point(87, 50);
Path path = new Path();
path.setFillType(FillType.EVEN_ODD);
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.moveTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.moveTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
canvas.drawPath(path, paint);
}
这是我得到的结果:
发布于 2014-03-27 22:18:20
我找到了答案
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.BLACK);
canvas.drawPaint(paint);
paint.setStrokeWidth(4);
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
Point a = new Point(0, 0);
Point b = new Point(0, 100);
Point c = new Point(87, 50);
Path path = new Path();
path.setFillType(FillType.EVEN_ODD);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
canvas.drawPath(path, paint);
}
发布于 2017-06-29 08:54:18
这个答案清楚地说明了@Egis在答案中给出的数字来自哪里。(这将绘制一个倒置的等边三角形,并用kotlin编写)
class TriangleView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
val paint = Paint()
val path = Path()
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas ?: return
canvas.drawPath(configurePath(canvas.width.toFloat(), path), configurePaint(paint))
}
fun getHeight(width: Double): Float {
return Math.sqrt((Math.pow(width, 2.0) - Math.pow((width / 2), 2.0))).toFloat()
}
fun configurePaint(paint: Paint): Paint {
paint.color = android.graphics.Color.WHITE
paint.isAntiAlias = true
return paint
}
fun configurePath(width: Float, path: Path): Path {
path.lineTo((width / 2f), getHeight(width.toDouble()))
path.lineTo(width, 0F)
path.lineTo(0f, 0f)
return path
}
}
get height函数为Pythagoras' Theorem,它将始终将等边三角形的高度定为其边长的87%
Gist可以在这里找到,它包含另一个方向的代码
发布于 2019-10-14 19:15:40
我最近创建了一个小的演示应用程序,绘制各种形状,包括三角形,矩形和螺旋线。这是绘制三角形的代码。有关完整的上下文,请参考该项目。在这款应用中,用户可以将三角形绘制为一幅图,也可以在用户滚动时将两个三角形组合成一个三角形。不断增加的三角形增长是通过三角形相似的原则完成的,每个小三角形最后都与大三角形相似。
项目:https://github.com/jdgreene2008/android_custom_views
private void drawTriangleShape(Canvas canvas, RectF bounds,
TriangleInterpolator triangleInterpolator, Paint paint) {
paint.setStyle(Paint.Style.FILL);
float baseInterpolation = triangleInterpolator
.getInterpolatedValues()[TriangleInterpolator.INTERPOLATION_VALUES_BASE];
float altitudeInterpolation = triangleInterpolator
.getInterpolatedValues()[TriangleInterpolator.INTERPOLATION_VALUES_ALTITUDE];
// *** Construct the Left Triangle ** //
// Bottom left vertex
float bottomLeftX = bounds.left;
float bottomLeftY = bounds.bottom;
// Bottom right corner
float bottomRightX = bottomLeftX + baseInterpolation;
float bottomRightY = bounds.bottom;
//Top Vertex
float topX = bottomRightX;
float topY = bottomRightY - altitudeInterpolation;
Path leftTriangle = new Path();
leftTriangle.lineTo(bottomLeftX, bottomLeftY);
leftTriangle.lineTo(bottomRightX, bottomRightY);
leftTriangle.lineTo(topX, topY);
leftTriangle.lineTo(bottomLeftX, bottomLeftY);
canvas.drawPath(leftTriangle, paint);
if (triangleInterpolator.isSymmetric()) {
// *** Construct the Right Triangle ** //
bottomLeftX = bounds.right - baseInterpolation;
bottomLeftY = bounds.bottom;
bottomRightX = bounds.right;
bottomRightY = bounds.bottom;
topX = bottomLeftX;
topY = bottomRightY - altitudeInterpolation;
Path rightTriangle = new Path();
rightTriangle.lineTo(bottomLeftX, bottomLeftY);
rightTriangle.lineTo(bottomRightX, bottomRightY);
rightTriangle.lineTo(topX, topY);
rightTriangle.lineTo(bottomLeftX, bottomLeftY);
canvas.drawPath(rightTriangle, paint);
}
}
https://stackoverflow.com/questions/20544668
复制相似问题