draw an image in the center of another rectangle using canvas - android

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);

Related

How to draw segmented circle like this with certain requirements

I can't just seem to figure it out. I am trying to draw a segmented circle (what looks like circle inside a circle). However I want the segments to have specific colors and to be transparent inside the smaller circle. Preferably , I would like to make the color of the segmented lines different than the circle
Here are the solutions I had in mind:
1- Draw arc with fill color for the bigger circle and draw a circle for the small circle. 2 problems with this. First one is that the inner circle area is no longer transparent as it takes the color from the bigger one. Second problem is that the segmentation lines of the outer circle is going all the way to the center (not only to the inner circle perimeter)
2) Draw arcs for the bigger outer circle and draw circle for the inner circle. Set it to be color filled but don't show strokes. Then draw another outer circle on top with no fill just to show strokes. And then draw lines between the inner and outer circle using the calculations ( angle and radius) to determine where the lines are... Very convoluted solution, there has to be another way. Even with this solution, still have problem with the color showing in the center but maybe playing with gradient can help.
I read so much on SO but I couldn't figure the right answer as many answers would remove the control of circle parameters
HEELP!!!
#Override
public void draw(Canvas canvas) {
float size = Math.min(getWidth(),getHeight());
paint.setStrokeWidth(size/4);
paint.setStyle(Paint.Style.STROKE);
final RectF oval = new RectF(0, 0, getWidth(), getHeight());
oval.inset(size/8,size/8);
paint.setColor(Color.RED);
Path redPath = new Path();
redPath.arcTo(oval, 0, 120, true);
canvas.drawPath(redPath, paint);
paint.setColor(Color.GREEN);
Path greenPath = new Path();
greenPath.arcTo(oval, 120, 120, true);
canvas.drawPath(greenPath, paint);
paint.setColor(Color.BLUE);
Path bluePath = new Path();
bluePath.arcTo(oval, 240, 120, true);
canvas.drawPath(bluePath, paint);
paint.setStrokeWidth(2);
paint.setColor(0xff000000);
canvas.save();
for(int i=0;i<360;i+=40){
canvas.rotate(40,size/2,size/2);
canvas.drawLine(size*3/4,size/2,size,size/2,paint);
}
canvas.restore();
final RectF ovalOuter = new RectF(0, 0, getWidth(), getHeight());
ovalOuter.inset(1,1);
canvas.drawOval(ovalOuter,paint);
final RectF ovalInner = new RectF(size/4, size/4, size*3/4,size*3/4);
canvas.drawOval(ovalInner,paint);
}
I'm drawing arcs using the Path class and strokes. Style.STROKE gives arcs without filling. Stroke width is set to size/4 which is a quarter of the view. Half of that stroke width goes outside and the second half goes inside, like this:
xxxxxxxx outer border of the arc of width 5
xxxxxxxx
------------ stroke
xxxxxxxx
xxxxxxxx inner border of the arc
That's why I'm using insets - I need to offset the stroke a bit in order to fit it in the view. Without insets the arcs are cut by all four sides of the view.
And why canvas rotation? Because it's easier to rotate the canvas with built-in methods than calculate lines manually. Rotation uses trigonometric functions and quickly becomes quite complex, hard to read and error prone. Basically I'm rotating the paper and drawing straight lines.

Android Canvas.drawBitmap(bmp, left, top, paint) after rotation

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?

How can I translate canvas after rotation to keep a rectangle in the center of another in android?

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

Set center of a drawable at a point

In a custom view, is there a way I can set the center of a drawable at a certain point on the canvas? (I don't want to deal with figuring out rectangular bounds for the drawable...)
When you make the call to draw the Bitmap, draw it at a point with it's left and top attributes set to the position minus half the image's width and height, respectively. That will center it at the point you want.
IE.
canvas.drawBitmap(bitmap, xPos - bitmap.getWidth()/2, yPos - bitmap.getHeight()/2, new Paint());

How to center an shape created through canvas?

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 ;)

Categories

Resources