I drew a rotated rectangle and I wanted to draw another one with the same size just above the first one and I can't get this done somehow the second rect don't get the right position
I give him the same left and right and change the bottom to the top of the first rect and the top to - top-top of the first rect
Here is the code for the first rect draw (the second draw is in my laptop and it is not near me)
int offset = 200;
int left = width/2;
int top = height/2;
int right = left + offset;
int bottom = top + offset;
canvas.save();
canvas.rotate(45, left + offset/2, top + offset/2);
canvas.drawRect(left, top, right, bottom, paint);
canvas.restore();
Related
Currently, we are developing one app which bases on face detection. when face detected on live camera we place another image left side of the detected area so it will look like a selfie picture.
so currently while taking a screenshot of camera view and drawn face area on canvas is showing black so we know capture screenshot of camera surface is not possible.
Does anyone have an idea to develop this type of application?
/**
* Draws the face annotations for position on the supplied canvas.
*/
#Override
public void draw(Canvas canvas) {
Face face = mFace;
if (face == null) {
return;
}
// Draws a circle at the position of the detected face, with the face's track id below.
float x = translateX(face.getPosition().x + face.getWidth() / 2);
float y = translateY(face.getPosition().y + face.getHeight() / 2);
// Draws a bounding box around the face.
float xOffset = scaleX(face.getWidth() / 2.3f);
float yOffset = scaleY(face.getHeight() / 2.3f);
float left = x - xOffset;
float top = y - yOffset;
float right = x + xOffset;
float bottom = y + yOffset;
canvas.drawRect(left, top, right, bottom, mBoxPaint);
// Bitmap to draw on the canvas
Bitmap bitmap = BitmapFactory.decodeResource(
resources,
R.drawable.ic_luncher);
sclledBitmp=getResizedBitmap(bitmap, (int)face.getWidth()-70, (int) face.getHeight()-70);
//Finally, Draw the source bitmap on the canvas
canvas.drawBitmap(
sclledBitmp, // Bitmap
right, // Left
top, // Top
null // Paint
);
canvas.drawText("Width: " + (int)face.getWidth() + " \nright:"+right, right, top, mIdPaint);
}
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
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);
}
I have to create a custom view where I have to draw a rectangle. I'm trying to use the canvas.drawRect method. I wanted to create rectangles somethig like this
the gray colored one is my custom view which is extending the View class.
Inside the onDraw method I'm trying to draw the rectangle.
But actually I'm confused with the parameters of the drawRect method.
As per the documentation
/**
* Draw the specified Rect using the specified paint. The rectangle will
* be filled or framed based on the Style in the paint.
*
* #param left The left side of the rectangle to be drawn
* #param top The top side of the rectangle to be drawn
* #param right The right side of the rectangle to be drawn
* #param bottom The bottom side of the rectangle to be drawn
* #param paint The paint used to draw the rect
*/
What I assume is that left and top forms the x,y coordinates of the starting point, and right is the width and bottom is the height. But it doesn't seem to work that way.
I tried something like this to draw one rectangle but it does not draw anything
paint.setColor(Color.BLUE);
canvas.drawRect(5, canvas.getHeight()/2, 30, 30, paint );
Can anyone please tell how exactly a rectangle is drawn using these values?
It would be very helpful if someone could show the code to draw at least the first rectangle.
My requirement is like, the number of inner rectangles are dynamic, so if I pass some 4 to this View, it should create 4 equal width rectangles horizontally. something like
Thanks in advance!!
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
}
}
drawRect(float left, float top, float right, float bottom, Paint paint)
It seems in your case the problem is that if right is less than left or bottom is less than top then the rectangle is not drawn. But it seems it happens only in some devices , as #David Medenjak commented
I also recommend you to use the View dimensions and not the Canvas dimensions, that is better to use getWidth() & getHeight() and not Canvas.getWidth() & Canvas.getHeight()
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
drawRectangle(posX, posY, 60, 40, canvas, paint)
I am trying to set the anchor point to the middle of the bitmap. By default it is always the top-left corner of the bitmap.
If I try:
canvas.drawBitmap(bitmap, x, y, paint);
It always starts drawing the bitmap from the top left. However it can be fixed by subtracting half of the width/height to the x/y point.
int widthOffset = bitmap.getWidth()/2;
int heightOffset = bitmap.getHeight()/2;
canvas.drawBitmap(bitmap, x-widthOffset, y-heightOffset, paint);
Is there any other solution which sets the anchor by default to the center of the bitmap?
Is there any way of drawing a Rect in Android in the following pattern, instead of "left, top, right, bottom"? The problem is that I'm using random coordinates:
Rect e = new Rect(random.nextInt(300)+20,20,random.nextInt(300)+45,70);
And it's hard to keep track of the first left random position to include in the right one to the width. In the "x, y, w, h", it's easy to apply my collision test:
if ((((p.left+p.width())>e.left)&&(p.left<(e.left+e.width())))&&
(((p.top+p.height())>e.top)&&(p.top<(e.top+e.height())))) {
gameScreen = 2;
}
Just move the random numbers outside of the constructor for your rect.
int left = random.nextInt(300)+20;
int right = left + 20;
int top = random.nextInt(300)+45;
int bottom = top + 70;
Rect e = new Rect(left, top, right, bottom);