-3

I need to draw a rectangle over an image so that user can select a specific part of that image when the user selects a rectangular part must be drawn over it.

for example say if the user wants click image if a parking lot then user can draw rectangle on parking space

1 Answers1

1

You have to override the onDraw() method on your view (ImageView), get the canvas and draw a rectangle. Something like that:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint myPaint = new Paint();
        int left = 10; // left padding from your view left border
        int top = 10; // top padding from your view top border
        int rectWidth = 50;
        int rectHeight = 30;
        myPaint.setColor(Color.rgb(0, 0, 0));
        myPaint.setStrokeWidth(10);
        canvas.drawRect(left, top, left + rectWidth, top + rectHeight, myPaint);
    }
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • You can take a look at this link, https://proandroiddev.com/android-draw-a-custom-view-ef79fe2ff54b basically you'll have to create a class that extends ImageView, override the onDraw() method and then include this class in your xml layout – Nicola Gallazzi May 14 '18 at 06:49
  • thanks its worked but i want to make it on rine time use can create it do u know any way like i can use gestures to make rectangle on 4 points like usre can drag it or move it – aahan verma May 14 '18 at 07:31
  • That's another topic, if you want to drag the rectangle and redraw basing on user gestures you have to override the onTouchEvent() method on your activity. Here for more details: https://developer.android.com/training/gestures/scale – Nicola Gallazzi May 14 '18 at 08:44
  • Please mark the response as complete and open another topic if you want support – Nicola Gallazzi May 14 '18 at 09:32