If you use the custom custom ViewView extension class, you need to call the canvas.invalidate () method, which will call the onDraw method for internal use. You can use the default API for the canvas to draw a circle. X, Y coordinates define the center of the circle. You can also define the color and style of the drawing and convey the drawing object.
public class CustomView extends View { public CustomView(Context context, AttributeSet attrs) { super(context, attrs); setupPaint(); } }
Define the default drawing parameters and the canvas (initialize the drawing in the constructor so that you can reuse the same object everywhere and change only certain parameters where necessary)
private Paint drawPaint; // Setup paint with color and stroke styles private void setupPaint() { drawPaint = new Paint(); drawPaint.setColor(Color.BLUE); drawPaint.setAntiAlias(true); drawPaint.setStrokeWidth(5); drawPaint.setStyle(Paint.Style.FILL_AND_STROKE); drawPaint.setStrokeJoin(Paint.Join.ROUND); drawPaint.setStrokeCap(Paint.Cap.ROUND); }
And initialize the canvas object
private Canvas canvas; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); this.canvas = canvas; canvas.drawCircle(xCordinate, yCordinate, RADIUS, drawPaint); }
And finally, for every view update or new drawing on the screen, you need to call the invalidate method. Remember that your entire review has been redrawn, so this is an expensive call. Make sure that you do only the necessary operations in onDraw
canvas.invalidate();
For more information on drawing on canvas, see https://medium.com/@mayuri.k18/android-canvas-for-drawing-and-custom-views-e1a3e90d468b.
mayuri Jan 11 '19 at 11:40 2019-01-11 11:40
source share