chikito1990
chikito1990

Reputation: 239

Draw rectangle with exact X and Y coordinates

I am developing an Android app which will insert rectangles dynamically. Th problem that occurred is that I have no idea how to insert a rectangle with specific X Y coordinates.

Here is the code so far:

public class MainActivity extends Activity {

DrawRectangle drawView;
ImageView DrawingImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView DrawingImage = (ImageView) this.findViewById(R.id.image2);

    Bitmap bitmap2 = Bitmap.createBitmap((int) getWindowManager()
        .getDefaultDisplay().getWidth(), (int) getWindowManager()
        .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap2);
    DrawingImage.setImageBitmap(bitmap2);

    // Draw Rectangle

    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.FILL);

    int left = 25;
    int top = 25;
    int right = 60;
    int bottom = 50;

    Point insertRec=new Point(0, 120);
    Rect rec = new Rect(insertRec.x, insertRec.y, insertRec.x+30, insertRec.y+25);
    canvas.drawRect(rec,paint);
    ImageView image = (ImageView) findViewById(R.id.image);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    image.setImageBitmap(bitmap);
}

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp"
        android:src="@drawable/test" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp" />

</FrameLayout>

I tried to put a new variable Point myPoint=new Point(x,y); but still I cannot insert it where I want because the insertion point is at (-60,120) and when I create that point it is not iserting there.

Any sugestion?

Thank you in advance!

EDIT

When I do it like that the rectangle does not appear at (0,120) because it is like 3-4cm inside of the screen.

Best regards, Dimitar Georgiev

Upvotes: 2

Views: 2803

Answers (1)

Quick learner
Quick learner

Reputation: 11457

Use this method to draw rectangle at X & Y coordinate with width and height params

fun drawRectangle(left: Int, top: Int, right: Int, bottom: Int, canvas: Canvas, paint: Paint?) {
        var right = right
        var bottom = bottom
        right = left + right // width is the distance from left to right
        bottom = top + bottom // height is the distance from top to bottom
        canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint!!)
    }

Usage

//drawing rectangle on x coordinate which is posX and y coordinate which is posY

        drawRectangle(posX, posY, 60, 40, canvas, paint)

Upvotes: 1

Related Questions