如何获取视图的绝对坐标 [英] How to get the absolute coordinates of a view

查看:23
本文介绍了如何获取视图的绝对坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取视图左上角的绝对屏幕像素坐标.但是,我可以找到的所有方法,例如 getLeft()getRight() 都不起作用,因为它们似乎都与视图的父级相关,因此给出我 0.这样做的正确方法是什么?

I'm trying to get the absolute screen pixel coordinates of the top left corner of a view. However, all methods I can find such as getLeft() and getRight() don't work as they all seem to be relative to the parent of the view, thus giving me 0. What is the proper way to do this?

如果有帮助,这是一个把图片放回原处"的游戏.我希望用户能够绘制一个框来选择多个部分.我的假设是,最简单的方法是从 MotionEvent 中获取 getRawX()getRawY(),然后将这些值与布局的左上角保存碎片.知道碎片的大小,我就可以确定选择了多少碎片.我意识到我可以在 MotionEvent 上使用 getX()getY() ,但因为它返回一个相对位置,可以确定哪些部分是选择难度较大.(并非不可能,我知道,但它似乎不必要地复杂).

If it helps, this is for a 'put the picture back in order' game. I want the user to be able to draw a box to select multiple pieces. My assumption is that the easiest way to do that is to getRawX() and getRawY() from the MotionEvent and then compare those values against the top left corner of the layout holding the pieces. Knowing the size of the pieces, I can then determine how many pieces have been selected. I realise I can use getX() and getY() on the MotionEvent, but as that returns a relative position that makes determining which pieces were selected more difficult. (Not impossible, I know, but it seems unnecessarily complicated).

根据其中一个问题,这是我用来尝试获取保存容器大小的代码.TableLayout 是包含所有拼图的表格.

This is the code I used to try to get the size of the holding container, as per one of the questions. TableLayout is the table which holds all the puzzle pieces.

TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
Log.d(LOG_TAG, "Values " + tableLayout.getTop() + tableLayout.getLeft());

编辑 2:这是我尝试过的代码,遵循了更多建议的答案.

Edit 2: Here is the code I've tried, following more of the suggested answers.

public int[] tableLayoutCorners = new int[2];
(...)

TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
tableLayout.requestLayout();
Rect corners = new Rect();
tableLayout.getLocalVisibleRect(corners);
Log.d(LOG_TAG, "Top left " + corners.top + ", " + corners.left + ", " + corners.right
            + ", " + corners.bottom);

cells[4].getLocationOnScreen(tableLayoutCorners);
Log.d(LOG_TAG, "Values " + tableLayoutCorners[0] + ", " + tableLayoutCorners[1]);

此代码是在所有初始化完成后添加的.图像已被划分为包含在 TableLayout 中的 ImageViews 数组(cells[] 数组).Cells[0] 是左上角的 ImageView,我选择了 Cells[4],因为它在中间的某个地方,绝对不应该有 (0,0) 的坐标.

This code was added after all the initialisation is done. The image has been divided up into a array of ImageViews (the cells[] array) contained within a TableLayout. Cells[0] is the top left ImageView, and I picked cells[4] as it's somewhere in the middle and most definitely should not have coordinates of (0,0).

上面显示的代码仍然让我在日志中全是0,我真的不明白,因为正确显示了各种拼图.(我为 tableLayoutCorners 和默认可见性尝试了 public int,两者都给出了相同的结果.)

The code shown above still gives me all 0s in the logs, which I really don't understand because the various puzzle pieces are correctly displayed. (I tried public int for tableLayoutCorners and default visibility, both giving the same result.)

我不知道这是否重要,但是 ImageView 最初没有给出大小.ImageViews 的大小是在初始化期间由视图自动确定的,当我给它一个要显示的图像时.这是否会导致它们的值为 0,即使记录代码是在它们被赋予图像并自动调整大小之后?为了潜在地解决这个问题,我添加了如上所示的代码 tableLayout.requestLayout(),但这并没有帮助.

I don't know if this is significant, but the ImageViews are originally not given a size. The size of the ImageViews is determined during the initialisation automatically by the View when I give it an image to display. Could this contribute to their values being 0, even though that logging code is after they have been given an image and have automatically resized themselves? To potentially counter that, I added the code tableLayout.requestLayout() as shown above, but that didn't help.

推荐答案

使用 View.getLocationOnScreen() 和/或 getLocationInWindow().

这篇关于如何获取视图的绝对坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆