我正在尝试创建一个五边形的多边形,可以通过拖动其一个顶点来调整其大小。我从this问题中尝试了一些有关SO的代码,但后来意识到该代码是专门为矩形而编写的。我尝试修改onDraw()方法中的代码,但后来在意识到它没有用之后将其重构为以前的方式。
有帮助吗?
答案 0 :(得分:0)
所以这是我刚刚编写的在画布上绘制五边形的代码。您几乎拥有可以在onTouchEvent上编辑的顶点。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int heigth = getHeight();
// not required, but just to move it to the middle
float centerX = width / 2.f;
float centerY = heigth / 2.f;
// vertices
float p1x = 100.f + centerX;
float p1y = 0.f + centerY;
float p2x = 0.f + centerX;
float p2y = -80.f + centerY;
float p3x = -100.f + centerX;
float p3y = 0.f + centerY;
float p4x = -80.f + centerX;
float p4y = 80f + centerY;
float p5x = 80.f + centerX;
float p5y = 80.f + centerY;
Path path = new Path();
path.moveTo(p1x, p1y);
path.lineTo(p2x, p2y);
path.lineTo(p3x, p3y);
path.lineTo(p4x, p4y);
path.lineTo(p5x, p5y);
path.lineTo(p1x, p1y);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawPath(path, paint);
}