Android View Hierarchy: Convert Relative to Absolute Coordinates
To convert the relative coordinates of child nodes to absolute coordinates and store them in an array, you can iterate through each child node and calculate their absolute position based on the position of their parent node.\n\nHere is an example code snippet to achieve this:\n\njava\nArrayList<Rect> childRects = new ArrayList<>();\n\nView parentView = findViewById(R.id.parent_view); // Replace with the ID of the parent view\n\nfor (int i = 0; i < parentView.getChildCount(); i++) {\n View childView = parentView.getChildAt(i);\n \n int[] childLocation = new int[2];\n childView.getLocationOnScreen(childLocation);\n \n int childLeft = childLocation[0];\n int childTop = childLocation[1];\n int childRight = childLeft + childView.getWidth();\n int childBottom = childTop + childView.getHeight();\n \n Rect childRect = new Rect(childLeft, childTop, childRight, childBottom);\n childRects.add(childRect);\n}\n\n// Now you have an array of Rect objects representing the absolute coordinates of each child node\n\n\nMake sure to replace R.id.parent_view with the actual ID of the parent view in your layout. Also, note that this code assumes that the parent view is already visible and laid out on the screen.
原文地址: https://www.cveoy.top/t/topic/n0Ze 著作权归作者所有。请勿转载和采集!