我正在尝试获取视图左上角的绝对屏幕像素坐标。 可是,我能够找到的全部方法getLeft()
例如getLeft()
和getRight()
都不起做用,由于它们彷佛都相对于视图的父级,所以给我0
。 正确的方法是什么? 数组
若是有帮助,则用于“按顺序放回图片”游戏。 我但愿用户可以绘制一个框来选择多个片断。 个人假设是,作到这一点的最简单方法是getRawX()
和getRawY()
从MotionEvent
,而后与布局保持件的左上角比较这些值。 知道了碎片的大小以后,我能够肯定选择了多少碎片。 我知道我可使用getX()
和getY()
在MotionEvent
,但做为一个返回相对位置,使得肯定哪一个被选择件更困难。 (我知道这不是没有可能,但彷佛没必要要地复杂)。 布局
编辑:根据问题之一,这是我用来尝试获取保存容器大小的代码。 TableLayout
是包含全部拼图的表格。 ui
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout); Log.d(LOG_TAG, "Values " + tableLayout.getTop() + tableLayout.getLeft());
编辑2:这是我尝试过的代码,遵循更多建议的答案。 this
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)的坐标。 spa
上面显示的代码仍然为我提供了日志中的全0,我真的不明白,由于正确显示了各个拼图。 (我尝试使用public int来获取tableLayoutCorners和默承认见性,二者的结果相同。) 日志
我不知道这是否有意义,可是ImageView
最初没有给出大小。 当我给ImageView
显示图像时,View会在初始化期间自动肯定ImageView
的大小。 即便该日志记录代码是在给它们一个图像并自动调整大小以后,这是否也会致使它们的值为0? 为了解决这个问题,我添加了代码tableLayout.requestLayout()
如上所示,但这没有帮助。 code
使用全局布局侦听器一直对我有效。 它的优势是,若是更改了布局(例如,若是将某些内容设置为View.GONE或添加/删除了子视图),则可以从新测量内容。 server
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); }
使用View.getLocationOnScreen()
和/或getLocationInWindow()
。 游戏
首先,您必须获取视图的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));
但愿这会有所帮助
遵循罗曼·盖伊(Romain Guy)的评论后,这就是我如何解决它。 但愿它能对遇到此问题的其余人有所帮助。
实际上,我确实在尝试获取视图的位置,而后再将其放置在屏幕上,但事实并不是如此。 这些行已在初始化代码运行后放置,所以我认为一切准备就绪。 可是,此代码仍在onCreate()中; 经过试验Thread.sleep(),我发现直到onCreate()到onResume()的全部执行完成后,布局才真正肯定下来。 因此确实,代码是在布局在屏幕上定位以前试图运行的。 经过将代码添加到OnClickListener(或某些其余侦听器)中,能够获取正确的值,由于只有在布局完成后才能将其触发。
建议将如下行做为社区编辑:
请使用onWindowfocuschanged(boolean hasFocus)
使用此代码查找确切的X和Y坐标:
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);
我添加/减去了一些值来调整个人观点。 请仅在放大整个视图后执行这些行。