Search code examples
androidandroid-canvaspaint

How do i draw only corners of a rectangle in android canvas


I want to draw on an canvas

enter image description here

How do i draw it on android canvas on the centre ? I tried using canvas.drawLine but didn't get the desired output


Solution

  • Image

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            Paint paint = new Paint(Paint.DITHER_FLAG);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(50);
            paint.setColor(0xFF2287BB);
    
            paint.setStrokeJoin(Paint.Join.MITER);
            canvas.drawPath(createCornersPath(getWidth()/2 - 500, getHeight()/2 - 500, getWidth()/2  +500, getHeight()/2 + 500, 150), paint);
        }
    
        private Path createCornersPath(int left, int top, int right, int bottom, int cornerWidth){
            Path path = new Path();
    
            path.moveTo(left, top + cornerWidth);
            path.lineTo(left, top);
            path.lineTo(left + cornerWidth, top);
    
            path.moveTo(right - cornerWidth, top);
            path.lineTo(right, top);
            path.lineTo(right , top + cornerWidth);
    
            path.moveTo(left, bottom - cornerWidth);
            path.lineTo(left, bottom);
            path.lineTo(left + cornerWidth, bottom);
    
            path.moveTo(right - cornerWidth, bottom);
            path.lineTo(right, bottom);
            path.lineTo(right, bottom - cornerWidth);
    
    
            return path;
        }