如何获取圆形的图像视图的中心
现在我有一个 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;
所以我通过“位置”获得的坐标是否是图像视图的顶点?
请帮助我如何获得图像视图的中心点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了 Chandan 的回答之外,
假设我们使用相同的代码:
如果您的
myImageView.getWidth()
返回 0,您可能需要进行一些修改:注意,您可能需要在创建 myImageView 之后执行此操作代码并添加到根视图。
Addition to Chandan's answer,
say we are using the same code:
if your
myImageView.getWidth()
is returning 0, you might want to do a little bit of modification:Notice, you might want to do this after myImageView is created in code and added to a root view.
我假设以下情况:
您将加载一个 ImageView(从 xml 或代码)并在 myImageView(比如说)中引用相同的内容。现在,您希望计算 myImageView 的中心,无论其在屏幕上的位置如何。
如果我的假设是正确的,那么下面可能就是您正在寻找的解决方案:
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:
您可以使用以下方法获取左上角坐标:
基于 height/2 和 width/2 您可以建立 ImageView 的中心。
You can get the top-left corner coordinates using:
Based on height/2 and width/2 you can establish the center of your ImageView .