题
我想得到绝对的屏幕素坐标的左上角的一个图。然而,所有的方法我可以找到这样的作为 getLeft()
和 getRight()
不工作他们都似乎是相对于父母的视,因此给我 0
.什么是适当的方式做到这一点?
如果有帮助的,这是一个'把照片回来了'游戏。我想让用户能够绘制一个框选择多个碎片。我的假设是,最简单的方法就是 getRawX()
和 getRawY()
从 MotionEvent
然后比较这些价值观对左上角的局保持。知道大小的碎片,然后我就可以确定有多少件已经选定。我意识到我可以用 getX()
和 getY()
在 MotionEvent
, 但如,返回一个相对位置,这使确定哪个选择了更多的困难。(不可能的,我知道,但它似乎不必要地复杂化).
编辑:这是码我曾试图获得大小的保存容器,为每一个问题。 TableLayout
是的表其拥有的所有拼图。
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
Log.d(LOG_TAG, "Values " + tableLayout.getTop() + tableLayout.getLeft());
编辑2:这里是代码,我已经试过了,下面的更多的建议的答案。
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]);
这个代码之后加入的所有初始化。图像已经被分割为一系列ImageViews(细胞[]array)内所包含的一个 TableLayout
.细胞[0]左上 ImageView
, 和我的细胞[4],因为它是某个地方在中间和大多数绝对不应该坐标为(0,0)。
代码以上所示仍然给我的所有0s在日志中,我真的不明白是因为各种拼图是正确的显示。(我试过公共int tableLayoutCorners和默认的知名度,给予同样的结果。)
我不知道如果这是重大的,但是 ImageView
s最初不给予的大小。尺寸的 ImageView
s是确定在初始化自动通过查看当我得到这个图像来显示。这可能有助于他们的价值为0,即使记录码之后,他们已经给出一个图像,并有自动调整自己?潜在的反,我加入代码 tableLayout.requestLayout()
如上所示,但这并没有帮助。
解决方案
其他提示
所接受的回答实际上并没有告知如何获得住所以这里是一个小的更详细的说明。你传递一个 int
阵列的长2和值被取代的观点是(x,y)坐标(在左上角).
int[] location = new int[2];
myView.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
注意到
- 替换
getLocationOnScreen
与getLocationInWindow
应该得到相同的结果在大多数情况下(见 这个答案).但是,如果你是在较小的窗口,就像一对话或定键盘上,然后利用你会需要选择哪一个更适合你的需要。 - 你会得到
(0,0)
如果你管这叫法onCreate
因为没有规定。你可以使用ViewTreeObserver
听布局时,你可以得到测量的坐标。(见 这个答案.)
首先你必须得到localVisible矩形图
对于例如:
Rect rectf = new Rect();
//For coordinates location relative to the parent
anyView.getLocalVisibleRect(rectf);
//For coordinates location relative to the screen/display
anyView.getGlobalVisibleRect(rectf);
Log.d("WIDTH :", String.valueOf(rectf.width()));
Log.d("HEIGHT :", String.valueOf(rectf.height()));
Log.d("left :", String.valueOf(rectf.left));
Log.d("right :", String.valueOf(rectf.right));
Log.d("top :", String.valueOf(rectf.top));
Log.d("bottom :", String.valueOf(rectf.bottom));
希望这将帮助
使用全球布局监听器一直工作很适合我。它的优点能够重新测量的事情如果布局改变时,例如如果有什么设置图。走了,或者孩子的看法是增加/删除。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is
LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null);
// set a global layout listener which will be called when the layout pass is completed and the view is drawn
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
//Remove the listener before proceeding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
// measure your views here
}
}
);
setContentView(mainLayout);
}
以下罗曼人的意见,这是我如何固定它。我们希望它将帮助的任何其他人也有这个问题。
我确实试图获得职位的意见之前,他们已经奠定了在屏幕上但这不是在所有明显正在发生的事情。这些线路已放置后initilisation码跑了,所以我假设一切准备就绪。然而,这个代码还在onCreate();通过试验线。sleep()我发现布局实际上不是最终定稿,直到后onCreate()所有的方式onResume()已经完成了执行。因此,事实上,代码是试图运行之前的布局完成了被定位在屏幕上。通过添加码到OnClickListener(或一些其他的听众)的正确值得的,因为它只能被解雇之后布局已经完成了。
线以下建议作为一个社区编辑:
请用 onWindowfocuschanged(boolean hasFocus)
第一种方式:
在 科特林 我们可以创建一个简单的扩展的观点:
fun View.getLocationOnScreen(): Point
{
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0],location[1])
}
而只是得到坐标:
val location = yourView.getLocationOnScreen()
val absX = location.x
val absY = location.y
第二种方式:
第二种方式是更简单的:
fun View.absX(): Int
{
val location = IntArray(2)
this.getLocationOnScreen(location)
return location[0]
}
fun View.absY(): Int
{
val location = IntArray(2)
this.getLocationOnScreen(location)
return location[1]
}
而简单地获得绝对X的 view.absX()
和Y的 view.absY()
我的工具的功能得到查看的位置,它将返回 Point
对象 x value
和 y value
public static Point getLocationOnScreen(View view){
int[] location = new int[2];
view.getLocationOnScreen(location);
return new Point(location[0], location[1]);
}
使用
Point viewALocation = getLocationOnScreen(viewA);
使用这个代码,找到确切的X和Y cordinates:
int[] array = new int[2];
ViewForWhichLocationIsToBeFound.getLocationOnScreen(array);
if (AppConstants.DEBUG)
Log.d(AppConstants.TAG, "array X = " + array[0] + ", Y = " + array[1]);
ViewWhichToBeMovedOnFoundLocation.setTranslationX(array[0] + 21);
ViewWhichToBeMovedOnFoundLocation.setTranslationY(array[1] - 165);
我已经增加/减去一些数值调整。 请不要这些线路只有在整个景已经膨胀。