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

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

问题描述

我试图让一个视图的左上角的绝对屏幕像素坐标。然而,所有的方法我能找到,如 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?

如果有帮助,这是一个把画面回以'游戏。我希望用户能够绘制一个框来选择多件。我的假设是,做到这一点的最简单的方法就是 getRawX() getRawY() MotionEvent 然后对布局保持件的左上角比较这些值。知道了碎片的大小,然后我就可以决定有多少个被选中。我知道我可以使用的getX() getY()以 MotionEvent ,但作为返回,使得确定哪些片选择更困难的相对位置。 (不是不​​可能的,我知道,但似乎过于复杂)。

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).

谢谢!

编辑:这是code我曾经试图得到保持容器的大小,按的问题之一。 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:这里是code我试过,下面更建议的答案

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]);

这code,加入所有的初始化完成后。图像被划分成ImageViews的包含在 TableLayout A阵列(细胞[]数组)。电池[0]是左上角的ImageView ,我挑细胞[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).

上面依然显示的code给我的日志中都为0,我真不明白,因为不同的拼图正确显示。 (我想公众诠释了tableLayoutCorners和默认可见性,既赋予相同的结果。)

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 取值原本没有给出一个尺寸。在 ImageView的大小 S是由我给它来显示图像查看自动初始化过程中确定。难道这有助于它们的值是0,即使该记录code是他们已获得的图像,并自动调整大小后的自己?潜在的计数器,我增加了code 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.

我又很为难......任何人谁可以帮助感谢。

I'm stumped... Thanks again to anyone who can help.

推荐答案

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

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

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