How to draw a circle inside a path on canvas in Android - android

I want to draw a circle inside a path on canvas. To draw a circle, center co-ordinates and radius are required.
How can I calculate the center co-ordinates and radius for a circle inside a path?

The path which you have created might be of irregular shape,it may not contain circle properly,anyways,
You can create a RectF from the Path and then get the center point of the RectF,
RectF mRectF = new RectF();
mOverlayPath.computeBounds(mRectF, true);//here, mOverlayPath is your path
float cX = mRectF.centerX();
float cY = mRectF.centerY();

Related

Find the center of a shape (path) on a canvas (Android Studio)

Is it possible to find the center of a shape on a canvas in Android studio? I've logged the touch points and can't seem to figure it out. It does however appear that the canvas (0,0) point is the top left, but the paths/shapes on the canvas see the center point as (0,0).
For an example of what I'm talking about check out the attached image, I'm trying to find where the green dot is.
Thanks in advance for any help.
IMG
To find the center of a Path use the method computeBounds(bounds,exact) which will set the first argument RectF bounds to the extent of the Path. Then it is just a matter of getting the mean of the left right and top bottom coordinates to get the geometric center of the path.
// mPath is your path. Must contain more than 1 path point
RectF bounds = new RectF();
mPath.computeBounds(bounds, false); // fills rect with bounds
PointF center = new PointF((bounds.left + bounds.right) / 2,
(bounds.top + bounds.bottom) / 2);
No Need for Mathematical Calculations
RectF bounds = new RectF();
mPath.computeBounds(bounds, false);
float centerX = rectF.centerX();
float centerY = rectF.centerY();

Scale Android Path

I am using a Path to draw a function to the Canvas. The problem I am facing is that the function is not scaled in the x axis. I tried using Canvas.sacale() in the onDraw() method but it made the path drawing blur. How can I stretch the path drawing in the x axis with out the blur.
If you try to scale the Canvas it will blur as if it is a bitmap. You should use some property from animation geometry to scale the Path.
Matrix matrix = new Matrix();
RectF rectF = new RectF();
your_path.computeBounds(rectF, true);
matrix.setScale(1.25f, 1f or 0f, rectF.centerX(), rectF.centerY());
your_path.transform(matrix);

Calculate points coordinate in canvas by axes

I need to make a rotating wheel on my android App. To do so, I'm creating a custom View in order to place it anywhere I want in the App activities.
All around the wheel, I need to place TextViews.
Thanks to the width and height of the view, I can get the center point of the canvas' view.
I know the angle, I know the radius, so now I need to place textviews on the edge of the circle by calculating the coordinates on the canvas.
Does anyone knows how to achieve that ?
Thanks in advance !
A point at angle theta on the circle whose center is (x0,y0) and whose radius is r is (x0 + r cos theta, y0 + r sin theta)
Or
Once you have drawn the circle you have to rotate the canvas put the text at required angle and then again restore it as,
canvas.save();
canvas.rotate(45, x, y);
canvas.drawText("your text here", x, y, paint);
canvas.restore();
Hope that helps..!!

How can i get the image matrix co ordinates in android?

I am developing an application where is included crop functionality and in this I'm blocked at below scenario.
The scenario:
Crop overlay is not circulating the whole image when we apply straightening to the image if we change the crop overlay, ratio also is not working.
I got the code from google but it is related to matrix, I have tried using matrix, but here I need to find the coordinates(edges) of matrix to move the overlay.
How to find it? If anyone has any idea please help me...
I added the image to explain the problem
Thanks in advance..
I am using the below code but I didn't get the exact coordinates of the matrix
RectF r = new RectF();
mymatrix.mapRect(r);
Rect rect =
new Rect((int)r.left,int)r.top,int)r.right+width,int)r.bottom+height);
You should not use mapRect if you are applying rotation on your matrix. My advice is to figure out the 4 initial points representing each rectangle edge (the image itself) and use mapPoints instead.
Lets say you have an image 200px wide and 200px tall with its top left corner positioned at origin (0,0).
If you rotate this image from its center (100,100) 45 degrees and then scale it 200% from its center we will have the following scenario:
//original image coords
float[] points = {
0f, 0f, //left, top
200f, 0f, //right, top
200f, 200f, //right, bottom
0f, 200f//left, bottom
};
Matrix matrix = new Matrix();
//rotate 45 degrees from image center
matrix.postRotate(45f, 100f, 100f);
//scale 200% from center
matrix.postScale(2f, 2f, 100f, 100f);
//get the new edges coords
matrix.mapPoints(points);
After calling matrix.mapPoints(points) the points array will have the updated coords. This means points[0], points[1] will have the new left top image coord and so on.
The even indices represents the abscissas and the odd indices represents the ordinates on the points array.

draw an image in the center of another rectangle using canvas

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

Categories

Resources