I am using android java to create an application that uses the accelerometer, i have created a shape that does kinda move with the accelerometer but it stays in the top left hand corner and moves there, can anyone help me with how can i move it to the center of the screen and set bounds
I am assuming you are using a canvas to draw that stuff if so canvas has a method called getwidth() and also getheight()
what you can do is when you are drawing it to the canvas set the location point x and y coordinates as
canvas.getWidth()/2
and canvas.getHeight()/2
something like this
canvas.drawBitmap(bitmap, canvas.getWidth()/2, canvas.getHeight()/2, null);
I hope you got the idea.
canvas.drawRect(left, top, right, bottom, paint)
use this function if you wanna draw a rectangle
Parameters:
left The left side of the rectangle to be drawn
top The top side of the rectangle to be drawn
right The right side of the rectangle to be drawn
bottom The bottom side of the rectangle to be drawn
paint The paint used to draw the rect
Use the same logic in this method and you are golden ;)
Related
I'm attempting to draw a rectangle around a custom view in Android. I mostly have it working except for one detail.
Here is my code...
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(14.5f);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRoundRect(0, 0, getWidth(), getHeight(), 20.0f, 20.0f, paint);
And here is the resulting rectangle...
As you can see, the inside of the rectangle does have rounded corners, but the outside still draws pointed corners. How can I make it so that the outside corners are rounded as well?
You don't see corners rounded on the outside because part of the stroke is outside of the bounds of the Canvas. You can check this easily by adding a certain margin to the coordinates of the round rectangle to make sure it is drawn inside the Canvas.
In fact your best option is trying to optimize this margin depending on the selected stroke width.
I'm building a drawing app in android and I noticed a problem while drawing a rectangle. the rectangle should be drawn according to the syntax
new Rect(left, top, right, bottom)
now my logic works by taking the start point (startX, startY) and end point (endX, endY) and I draw using:
new Rect(startX,startY, endX, endY);
assuming the user is drawing from the top left corner to the bottom right.
The Problem
now the problems happens when the user tries to draw the rectangle form right to left or from bottom to top. working on the first problem (right to left):
it means the startX is now pointing to the right (rather than left). so the code will be trying to draw with this syntax now:
new Rect(right, top, left, bottom)
Which will fail to draw the rectangle.
In other words, startX is larger than endX which shouldn't be while drawing the rectangle.
Resolution
so I used the following code to check before drawing the rectangle:
int smallX,largeX,smallY,largeY;
if(startX>endX){ smallX=endX; largeX=startX; }else{ smallX=startX; largeX=endX; }
if(startY>endY){ smallY=endY; largeY=startY; }else{ smallY=startY; largeY=endY; }
rect = new Rect(smallX, smallY,largeX,largeY);
And that will ensure than the first parameter will always point to the left and the others are good as well.
My question here... Is there another way around to fix this problem?
I don't think so, because Canvas is settted by coordinates and the coordinate (0,0) is top left and grow to bottom right, and the renderer follow that logic, start top left an go line after line to the bottom.
Is there a detailed documentation explaining the left/top parameter used in the canvas.drawBitmap method?
I thought it should always be the position relative to left-top corner of the canvas. For example, if I want to draw something at the top left corner, I just draw from origin (0,0).
However, I found this is not the case if the canvas is rotated 90 degree.
canvas.save();
canvas.rotate(90, canvas.getWidth()/2, canvas.getHeight()/2);
canvas.drawBitmap(bmp, 0, 0, null);
canvas.restore();
I was expecting the bitmap should be drawn from the left top corner on the rotated canvas, and displayed from right top corner after I restore the canvas, but it comes out different result.
I have to draw at
x= (canvas.getWidth()-canvas.getHeight())/2
y= (canvas.getHeight()-canvas.getWidth())/2
to get the expected result, why?
I want to be able to draw text and rectangles to the canvas after I have rotated them along their center. So I am making a test case where I have a blue square that does not get rotated, and a red square that should be rotated. THey are the same size, and should share the same center "pivot point". I have the following code:
Paint p = new Paint();
p.setColor(Color.CYAN);
p.setAlpha(200);
canvas.drawRect(new Rect(100,100,300,300), p);
canvas.save();
canvas.rotate(45,250,250);// 250,250 is the center of the blue rectangle
p.setColor(Color.RED);
p.setAlpha(100);
canvas.drawRect(new Rect(100,100,300,300), p);
canvas.restore();
It gives me a result close to what I want, but I am missing some math, because it looks like the canvas also needs a translation applied. Here is the result:
What I am missing so that I can rotate the red rectangle along the center of the blue one, and they end up sharing the same center point like this:
The center of the blue rectangle is wrong.
center(x,y) = (left + (width/2), top + (height/2))
Note: width = 200, height = 200 so, center(x,y) = (200,200)
Change to this, and it works:
canvas.rotate(45,200,200);// 200,200 is the center of the blue rectangle
In my view I have a big rectangle, the rectangle can move. When the rectangle moves to someplace, I want to draw image in the center of the big rectangle. My question is that I cannot put the center of the image to the center of the rectangle.
I used:
canvas.drawBitmap(rotatedBitmap, matrix, paint)
canvas.drawBitmap(rotatedBitmap, left, top, paint)
but i cannot find canvas.drawBitmap(rotatedBitmap, centerX, centerY, paint), so I want to use matrix, but the matrix also moves image from the left and top start, not from center, can you give some clue to draw the pic in the center of the rectangle?
Try using the bounds of the rectangle as a reference point, then use something like:
imageStartX = (rectStartX + (rectWidth/2)) - (imageWidth/2);
imageStartY = (rectStartY + (rectHeight/2)) - (imageHeight/2);