我正在进行一些原型设计以通知我的代码,但我遇到了一个问题,我无法确定来源。我试图在屏幕上的所有可访问性可点击项目周围绘制矩形(框架),但我的框架最终会出现水平移动。这种转变似乎是不变的,所以我知道我可以把它作为一个黑客来抵消它,但是因为我试图更好地理解这个领域,所以我已经知道它的来源了。这是我的代码。
在屏幕上的所有视图上调用的函数:
private void drawFrameIfClickable(AccessibilityNodeInfo accessibilityNodeInfo) {
boolean clickable;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
clickable = accessibilityNodeInfo.getActionList().contains(AccessibilityNodeInfo.AccessibilityAction.ACTION_CLICK);
} else {
clickable = (accessibilityNodeInfo.getActions() & AccessibilityNodeInfo.ACTION_CLICK) > 0;
}
if (clickable) {
Rect rect = new Rect();
accessibilityNodeInfo.getBoundsInScreen(rect);
View view = new FrameView(mas, rect);
ViewGroup.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
rect.left, rect.top,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
cursorParams.gravity = Gravity.TOP | Gravity.LEFT;
windowManager.addView(view, params);
highlights.add(view);
}
}
private class FrameView extends View {
private final Paint paint = new Paint();
private Rect rect;
public FrameView(Context mas, Rect rect) {
super(mas);
this.rect = rect;
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawRect(0, 0, rect.width(), rect.height(), paint);
}
}
' rect'中的值传递给视图的是"正确",即他们同意开发工具"指针位置"所检查的可点击项目的位置。但是一旦绘制,水平偏移就在那里,如下所示: