如何获取圆形的图像视图的中心

发布于 2024-11-19 17:50:57 字数 412 浏览 2 评论 0原文

现在我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平定天下 2024-11-26 17:50:58

除了 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;

注意,您可能需要在创建 myImageView 之后执行此操作代码并添加到根视图。

Addition to Chandan's answer,
say we are using the same code:

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

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

if your myImageView.getWidth() is returning 0, you might want to do a little bit of modification:

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

Notice, you might want to do this after myImageView is created in code and added to a root view.

小巷里的女流氓 2024-11-26 17:50:57

我假设以下情况:

您将加载一个 ImageView(从 xml 或代码)并在 myImageView(比如说)中引用相同的内容。现在,您希望计算 myImageView 的中心,无论其在屏幕上的位置如何。

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

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

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

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;
a√萤火虫的光℡ 2024-11-26 17:50:57

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

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

基于 height/2 和 width/2 您可以建立 ImageView 的中心。

You can get the top-left corner coordinates using:

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

Based on height/2 and width/2 you can establish the center of your ImageView .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文