drawRect Android Canvas

Asked

Viewed 1,247 times

0

I can’t understand how the canvas.drawRect on android.

canvas.drawRect(left, top, right, bottom, Paint)

I "come" from the HTML5 canvas that would be.

(context). fillRect(placeX, placeY, width, height)

The question is I wanted to know how to determine X position, Y position, width, height rectangle on android canvas ?

  • gives a look at Veloper.android.com/Reference/android/Graphics/Canvas.html this explaining.

1 answer

2


Expensive, to use the function canvas.drawRect(left, top, right, bottom, paint) you should consider the following:

  • left: coordinated x upper left corner of rectangle;
  • top: coordinated y upper left corner of rectangle;
  • right: coordinated x right bottom corner of rectangle;
  • bottom: coordinated y right bottom corner of rectangle;
  • Paint: Paint object that determines the characteristics of your rectangle, such as the color.

Remembering that you should keep in mind the java coordinate system relative to the View where you will draw your rectangle.

So if you want to draw a rectangle that starts at the coordinates x=100 and y=100 and has a width of 400 and a height of 900 you can use the following call:

...
canvas.drawRect(100f, 100f, 500f, 1000f, paint);
...

I don’t know exactly where and how you will use this, but below follows a very simple example that you can run and do some tests.

  • activity_main.xml

    <LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    
    <ImageView android:id="@+id/resulting_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"/>
    
    <Button android:id="@+id/btn_create_square"
        android:text="Create Square"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

  • Mainactivity.java

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

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

        mImageView = (ImageView) findViewById(R.id.resulting_image);

        Button button = (Button) findViewById(R.id.btn_create_square);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createSquare();
            }
        });
    }

    //Este é o método que efetivamente desenha o retângulo
    private void createSquare() {
        Bitmap bitmap = Bitmap.createBitmap(mImageView.getWidth(),
                mImageView.getHeight(),
                Bitmap.Config.ARGB_8888);
        bitmap = bitmap.copy(bitmap.getConfig(), true);

        Canvas canvas = new Canvas(bitmap);

        //configuracao do obj Paint com as características do retângulo
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        //desenhando...
        canvas.drawRect(100f, 100f, 500f, 1000f, paint);

        mImageView.setImageBitmap(bitmap);
    }
}

  • Upshot:

Desenhando um retângulo simples em uma ImageView

I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.