-1

I have Rect and Point. I want to draw rectangle in android studio. But I don't know how it is possible. I was doing extract text from image and make rectangle on it. Extracting text is successfully done, but don't know the syntax for drawing rectangle.

Ajay K S
  • 350
  • 1
  • 5
  • 16
  • 1
    check this https://developer.android.com/reference/android/graphics/Rect – Ajay K S Jan 09 '20 at 06:32
  • Does this answer your question? [Android canvas draw rectangle](https://stackoverflow.com/questions/7344497/android-canvas-draw-rectangle) – isaaaaame Jan 09 '20 at 07:29

2 Answers2

0

Hello @syend the easier way is to use vector Drawable and implement

        <?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="200dp"
android:width="300dp"
android:viewportHeight="100"
android:viewportWidth="100">

<path

    android:fillColor="@android:color/holo_orange_dark"
    android:pathData="M 0,0 L 100,0 L 100,100  0,100 z" />

 </vector>

In the layout activity please

 <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

app:srcCompat="@drawable/rectange_shape"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

The best option is to use Custom View by Create the Class and extends the View and to draw the Rectangle you can use

Rect drawRect = new Rect(x, y, x + (int) mShapeSize, y + (int) mShapeSize);
0

i would use a Canvas

Add this class to your project

public class MyCanvas extends View {

    private Paint paint;
    private Rect rectangle


    public MyCanvas(Context context) {

        super(context);
        paint = new Paint();

    }



    public MyCanvas(Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);

    }



    public MyCanvas(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

    }



    @Override

    protected void onDraw(Canvas canvas) {



        canvas.drawRect(rect, paint)

    }





    public void setRect(Rect rect) {

        this.rectangle = rect;

    }



    public Rect getRect() {

        return rectangle;

    }


    add this to your Activity

MyCanvas canvas = new Canvas(this);
canvas.setRect(yourRectangle)

and with

canvas.invalidate()

you call the onDraw-Method in your canvas so you when you Change your rectangle with setRect(yourRectangle) allwasy call invalidate()

}

DoFlamingo
  • 232
  • 1
  • 18