Ответ 1
Используя то, что @pskink объяснено в комментариях выше, вот как я достиг этого:
Сначала я выполнил эту команду:
adb shell dumpsys activity top
Затем я использовал этот код для его анализа:
public class ViewCoordsGetter {
public static Rect getViewBoundyingBox(String viewIdStr) {
final List<String> viewHierarchyLog = //result of the command
for (int i = 0; i < viewHierarchyLog.size(); ++i) {
String line = viewHierarchyLog.get(i);
if (line.contains(":id/" + viewIdStr + "}")) {
Rect result = getBoundingBoxFromLine(line);
if (i == 0)
return result;
int currentLineStart = getStartOfViewDetailsInLine(line);
for (int j = i - 1; j >= 0; --j) {
line = viewHierarchyLog.get(j);
if ("View Hierarchy:".equals(line.trim()))
break;
int newLineStart = getStartOfViewDetailsInLine(line);
if (newLineStart < currentLineStart) {
final Rect boundingBoxFromLine = getBoundingBoxFromLine(line);
result.left += boundingBoxFromLine.left;
result.right += boundingBoxFromLine.left;
result.top += boundingBoxFromLine.top;
result.bottom += boundingBoxFromLine.top;
currentLineStart = newLineStart;
}
}
return result;
}
}
return null;
}
private static int getStartOfViewDetailsInLine(String s) {
int i = 0;
while (true)
if (s.charAt(i++) != ' ')
return --i;
}
private static Rect getBoundingBoxFromLine(String line) {
int endIndex = line.indexOf(',', 0);
int startIndex = endIndex - 1;
while (!Character.isSpaceChar(line.charAt(startIndex - 1)))
--startIndex;
int left = Integer.parseInt(line.substring(startIndex, endIndex));
startIndex = endIndex + 1;
endIndex = line.indexOf('-', startIndex);
endIndex = line.charAt(endIndex - 1) == ',' ? line.indexOf('-', endIndex + 1) : endIndex;
int top = Integer.parseInt(line.substring(startIndex, endIndex));
startIndex = endIndex + 1;
endIndex = line.indexOf(',', startIndex);
int right = Integer.parseInt(line.substring(startIndex, endIndex));
startIndex = endIndex + 1;
//noinspection StatementWithEmptyBody
for (endIndex = startIndex + 1; Character.isDigit(line.charAt(endIndex)); ++endIndex)
;
int bot = Integer.parseInt(line.substring(startIndex, endIndex));
return new Rect(left, top, right, bot);
}
}