public class TriangleView extends View {
private Paint paint;
private float x0, y0; // 顶点坐标
private float L; // 边长
public TriangleView(Context context, float x0, float y0, float L) {
super(context);
this.x0 = x0;
this.y0 = y0;
this.L = L;
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLACK); // 设置颜色
paint.setStyle(Paint.Style.STROKE); // 设置空心效果
paint.setStrokeWidth(5); // 设置画笔宽度
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 计算其他两个顶点
float x1 = x0 + L * (float) Math.cos(Math.toRadians(30));
float y1 = y0 + L * (float) Math.sin(Math.toRadians(30));
float x2 = x0 + L * (float) Math.cos(Math.toRadians(150));
float y2 = y0 + L * (float) Math.sin(Math.toRadians(150));
// 绘制线条
canvas.drawLine(x0, y0, x1, y1, paint);
canvas.drawLine(x1, y1, x2, y2, paint);
canvas.drawLine(x2, y2, x0, y0, paint);
}
}