Android canvas draw rectangle
Try paint.setStyle(Paint.Style.STROKE)
?
How canvas.drawRect draws a rectangle
But actually I'm confused with the parameters of the drawRect method.
The drawRect
method requires only two coordinates to draw a rectangle.
The top left corner and the bottom right corner. So the 4 points form these 2 coordinates
in your canvas. Hope it is clear from the below images
P1 and P2 are points formed by (left,top) and (right,bottom), So the drawn rectangle would be like this.
To draw rectangles dynamically like you have shown in you image , try something like this
int[] colors = new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW}; // given some fixed colors
in you onDraw
method
@Override
protected void onDraw(Canvas canvas) {
int padding = 5;
float rectangleWidth = (getMeasuredWidth() - padding * 2) / colors.length;
for (int i = 0; i < colors.length; i++) {
paint.setColor(colors[i]);
canvas.drawRect(padding + (rectangleWidth * i),
getMeasuredHeight() / 2,
padding + rectangleWidth * (i + 1),
getMeasuredHeight() - padding, paint
); // 5 px is the padding given to the canvas
}
}
how to draw rectangle canvas?
You need to make method and pass value from activity to draw class:-
Draw draw = new Draw(this,100, 300, 600, 800);
constraintLayout.addView(draw);
Draw class
public class Draw extends View {
Paint paint;
Path path;
float left;
float top;
float right;
float bottom;
public Draw(Context context,float left, float top, float right, float bottom) {
super(context);
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
init();
}
public Draw(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Draw(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init(){
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawRect(left, top, right, bottom, paint);
}
}
Drawing rectangles on android canvas
Assuming that you only want this to be for this specific pattern and not expandable then the following should work. Though it is unlikely that it will look right because it is unlikely that a screen will be 1:2 aspect ratio, so you might need to make your canvas a set size that conforms to a 1:2 aspect ratio.
int width =canvas.getWidth();
int height = canvas.getHeight();
float a = (height*3)/5;
flaot b = (height*2)/5;
float c = (width*3)/5;
float d = (width*4)/5;
//5x5 square
canvas.drawRect(0,a,width,height);
//3x3 square
canvas.drawRect(0,0,a,c);
//2x2 square
canvas.drawRect(c,0,width,b);
//first 1x1 square
canvas.drawRect(d,b,width,a);
//second 1x1 square
canvas.drawRect(c,b,d,a);
The variables a,b,c,d are explained in the following image
The parameters in drawRect
are the coordinates of the corresponding edge of the rectangle in the canvas coordinate system. The canvas coordinate system has (0,0) in the top left of the screen. So if you wanted a triangle to fill the whole screen you would have canvas.drawRect(0,0,canvas.get(width),canvas.getheight());
This would put the left edge of the rectangle along 0 in the x axis, the top along 0 in the y axis, the right edge along the x axis at the width of the canvas and the bottom along the y axis at the height of the canvas.
Hope this helps.
How to use android canvas to draw a Rectangle with only topleft and topright corners round?
You can draw that piece by piece using drawLine()
and drawArc()
functions from the Canvas
.
Understanding how actually drawRect or drawing coordinates work in Android
X runs horizontally, from left to right. Y runs vertically, from top to bottom. It's exactly the same as on your graphics. So (0/0) is at top left.
When you go "up" Y will of course get smaller, as it grows from top to bottom.
You have to pay attention to laying out elements like ListViews, these will give a partial (or new, you cannot tell) canvas to your views that are drawn. These views will have 0x0 at their own top/left position. If you need the absolute you have to subsequently call View.getLocationOnScreen()
and calculate offsets yourself.
Drawing a filled rectangle with a border in android
You draw a rectangle with the color of the border and the size of the rectangle plus the border, you change the color of the paint and draw again the rectangle with the normal size.
How do i draw only corners of a rectangle in android canvas
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;
}
Draw canvas drawRect with dynamic co ordinates
You are copying the Bitmap into memory and never displaying, draw on the one that you set as ImageResource:
setImageBitmap(bitmap);
Canvas canvas = new Canvas(bitmap);
If you need to copy as ARGB_8888 then
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(drawableBitmap);
//Draw everything, then after (at end of method)
setImageBitmap(drawableBitmap);
As for your coordinates:
X Y Width Height
600, 100, 50, 50
440, 125, 50, 50
685, 270, 50, 50
420, 350, 50, 50
...
They are (X,Y,W,H) while Android Rectangles are (L,T,R,B)
To convert your coordinates use:
Rectangle area = new Rectangle(x, y, x + w, y + h);
Then draw the area into the Canvas.
Related Topics
How to Make an Android App to Always Run in Background
How to Pause/Resume Thread in Android
Asynctask and Looper.Prepare() Error
Gradle Build Tool Cannot Find Play-Services-Tasks.Aar? Why
Android - Drawable with Rounded Corners at the Top Only
Setuservisiblehint Called Before Oncreateview in Fragment
How to Convert Views to Bitmaps
How to Call a Local Web Service from an Android Mobile Application
How to Simulate :Active CSS Pseudo Class in Android on Non-Link Elements
How to Change Status Bar Color to Match App in Lollipop? - Android
Android Image View Pinch Zooming
Comparing Two Drawables in Android
How to Call a Method in Another Activity from Activity
Making Data Persistent in Android
Listview with Add and Delete Buttons in Each Row in Android
How to Get the List of Mounted External Storage of Android Device