我已经在Android上工作了一段时间,想知道是否有可能在android中检索按钮的位置.
我的目标是获取X和Y坐标并将其打印在LOGCAT上.
一些例子告诉我如何欣赏.
谢谢
Azl*_*lam 14
当然,您可以获得这些,确保在您尝试获取位置之前至少绘制一次视图.您可以尝试获取onResume()中的位置并尝试这些函数
view.getLocationInWindow()
or
view.getLocationOnScreen()
Run Code Online (Sandbox Code Playgroud)
或者如果你需要与父母相关的东西,请使用
view.getLeft(), view.getTop()
Run Code Online (Sandbox Code Playgroud)
API定义的链接:
像Azlam一样,你可以使用View.getLocationInWindow()来获取坐标x,y.
这是一个例子:
Button button = (Button) findViewById(R.id.yourButtonId);
Point point = getPointOfView(button);
Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")");
private Point getPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new Point(location[0], location[1]);
}
Run Code Online (Sandbox Code Playgroud)
奖金 - 获取给定视图的中心点:
Point centerPoint = getCenterPointOfView(button);
Log.d(TAG, "view center point x,y (" + centerPoint.x + ", " + centerPoint.y + ")");
private Point getCenterPointOfView(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0] + view.getWidth() / 2;
int y = location[1] + view.getHeight() / 2;
return new Point(x, y);
}
Run Code Online (Sandbox Code Playgroud)
我希望这个例子对某人有用.
| 归档时间: |
|
| 查看次数: |
17488 次 |
| 最近记录: |