Android 如何获得圆形图像视图的中心

Android 如何获得圆形图像视图的中心,android,touch-event,android-imageview,Android,Touch Event,Android Imageview,现在我有一个ImgeView,它是一个圆,我想找到这个圆的中心点,所以我尝试了下面的代码,但它似乎不起作用 int[] location = new int[2]; imageView.getLocationOnScreen(location); int radius=imageView.getHeight()/2; int[] center=new int[2]; center[0]=lo

现在我有一个ImgeView,它是一个圆,我想找到这个圆的中心点,所以我尝试了下面的代码,但它似乎不起作用

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;
所以我通过“位置”得到的坐标是否是imageview的顶点


请帮助我如何获取图像视图的中心点。

您可以使用以下方法获取左上角坐标:

ImageView iv = (ImageView)findViewById(R.id.image_view);
Rect rect = iv.getDrawable().getRect();
int xOffset = rect.left;
int yOffset = rect.top;

根据高度/2和宽度/2,可以建立图像视图的中心

我假设了以下情况:

您将加载一个ImageView(来自xml或代码),并在myImageView中引用它(比如)。现在,您希望计算myImageView的中心,而不考虑其在屏幕上的位置

如果我的假设是正确的,那么下面可能是您正在寻找的解决方案:

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

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

除了Chandan的回答, 假设我们使用相同的代码:

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

int centerXOfImageOnScreen=myImageView.getLeft()+centerXOnImage;
int centerYOfImageOnScreen=myImageView.getTop()+centerYOnImage;
如果您的
myImageView.getWidth()
返回0,您可能需要做一些修改:

myImageView.measure(0,0);
int centerXOnImage=myImageView.getMeasuredWidth()/2;
int centerYOnImage=myImageView.getMeasuredHeight()/2;

注意,在MyVieview被创建并添加到根视图之后,你可能想这样做。

我想要中心点的x、y坐标和整个屏幕方向,因为我必须考虑整个屏幕工作在IVieview的中心点,但是MyIVieView。是否会根据其上级给出职位???其实我想实现以下链接目标****也许你可以更深入地理解我的问题MyImageView.getHeight()始终返回0。为什么?