Devon Smith Devon Smith - 4 days ago 5
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];
imageView.getLocationOnScreen(location);

int radius=imageView.getHeight()/2;
int[] center=new int[2];
center[0]=location[0]+radius;
center[1]=location[1]+radius;


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

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