OnAccessibilityEvent:AccessibilityNodeInfo =>如何知道每个元素的Rect.centerX和Rect.centerY?

时间:2019-01-12 15:06:56

标签: android accessibilityservice

我想知道在阅读活动视图时如何发现每个发现的元素Rect.centerX, Rect.centerY吗?

这是我的能够读取活动视图的代码:

protected void onServiceConnected() {
    super.onServiceConnected();

    AccessibilityServiceInfo tempInfo = getServiceInfo();
    tempInfo.flags |= AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
    setServiceInfo(tempInfo);

}

//-------------------------------------------------------------------------------

public static void logNodeHeirarchy(AccessibilityNodeInfo nodeInfo, int depth) {

    if (nodeInfo == null) return;

    String logString = "";

    for (int i = 0; i < depth; ++i) {
        logString += " ";
    }

    logString += "Text: " + nodeInfo.getText() + " " + " Content-Description: " + nodeInfo.getContentDescription();

    Log.v(LOG_TAG, logString);

    for (int i = 0; i < nodeInfo.getChildCount(); ++i) {
        logNodeHeirarchy(nodeInfo.getChild(i), depth + 1);
    }
}

@Override
public void onAccessibilityEvent(AccessibilityEvent e) {

    switch (e.getEventType()) {
        case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
            logNodeHeirarchy(getRootInActiveWindow(), 0);
        }
    }

1 个答案:

答案 0 :(得分:0)

解决方案:


对于每个nodeInfo,我们可以使用getBoundsInScreen()函数来填充作为参考传递的Rect变量。然后,在此之后,我们便可以分别获取到分别指向Rect.centerX()Rect.centerY()的值。

例如:

public static void logNodeHeirarchy(AccessibilityNodeInfo nodeInfo, int depth) {

    if (nodeInfo == null) return;

    String logString = "";

    for (int i = 0; i < depth; ++i) {
        logString += " ";
    }

    ////////////////////////////////////

    Rect rect = new Rect();

    nodeInfo.getBoundsInScreen(rect);

    ///////////////////////////////////

    logString += " Text: " + nodeInfo.getText() + " " + " Content-Description: " + nodeInfo.getContentDescription() + " " + " RectCenterX: " + String.valueOf(rect.centerX()) + " " + " RectCenterY: " + String.valueOf(rect.centerY());

    Log.v(LOG_TAG, logString);

    for (int i = 0; i < nodeInfo.getChildCount(); ++i) {
        logNodeHeirarchy(nodeInfo.getChild(i), depth + 1);
    }
}