Good day. You need to dynamically draw a polygon in android on canvas. I do like this, but nothing comes out !!! Where is the mistake?
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); path.moveTo(startX,startY); for (i=0;i<x.length;i++){ x[i]=startX; y[i]=startY; path.lineTo(x[i], y[i]); path.close(); canvas.drawCircle(x[i], y[i], 20, paint); } canvas.drawPath(path,paint); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: startX= event.getX(); startY= event.getY(); break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_MOVE: break; default: return false; } invalidate(); return super.onTouchEvent(event); }
path.close()probably should not be called in a loop, but before drawing. Although everything is generally sad. You need to store the index of the next point in the array in the array, fill it with coordinates inonTouch, increase the index, and inonDrawdraw fromx[0], y[0]to the end. It may be better to use lists instead of arrays. - zRrr