Devon Smith Devon Smith - 10 months ago 68
Android Question

How to get Center of an Imageview which is a circle

Now I have an ImgeView which a circle and I want to find the center point of that circle so I have tried the following code but it seems not working

int[] location = new int[2];

int radius=imageView.getHeight()/2;
int[] center=new int[2];

so the coordinates I got by "location" is top point of the imageview or not ?

Please help me how to get a center point of an imageview.

Answer Source

I assumed below situation:

You will be loading an ImageView(either from xml or code) and have a reference to the same in myImageView(say). Now, you wish to calculate the centre of the myImageView irrespective of its position on the screen.

If my assumption is correct then probably below is the solution you are looking for:

    int centerXOnImage=myImageView.getWidth()/2;
    int centerYOnImage=myImageView.getHeight()/2;

    int centerXOfImageOnScreen=myImageView.getLeft()+centerXOnImage;
    int centerYOfImageOnScreen=myImageView.getTop()+centerYOnImage;