在Android版本> = 5上绘制导航栏(和其他应用程序)

mih*_*iha 10 android navigationbar android-fullscreen

我想在屏幕上绘制一个(鼠标指针)图标,而不是服务中的其他应用程序.我已经实现了功能,除了导航栏,我可以在屏幕上绘图.我研究过一些 其他 的问题在这里和尝试TYPE_SYSTEM_OVERLAY,TYPE_TOAST,TYPE_SYSTEM_ERROR和一些其他的窗口类型没有成功.

我不是想捕捉焦点,只是在屏幕上画一两秒钟.当我尝试绘制导航栏时,它只是在下面(实际上,RelativeLayout在导航栏的边界上结束 - 即使我指定高度的手动尺寸).下面的屏幕截图显示了屏幕右下方的手形指针.这是我可以定位的最低点.请注意,我不是要在我的应用程序中隐藏导航栏 - 试图绘制其他应用程序.

叠加指针在屏幕上

我甚至尝试在实例中设置xposypos偏移设置WindowManager.LayoutParams,但这只是偏移布局并仍然在导航栏下方).

我用来显示这个窗口的布局参数:

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)

然后我将RelativeLayout这些参数添加到WindowManager:windowManager.addView(relativeLayout, params);

mih*_*iha 10

经过大量的摆弄,我已经成功地获得了正确的旗帜以使其发挥作用.它几乎适用(对于大多数应用程序).要实现的代码如下(按钮单击处理程序的示例):

@Override
public void onClick(View view) {
    if (_windowManager == null) {
        _windowManager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
    }

    WindowManager.LayoutParams params;
    // create the view on the first try
    if (_overlayView == null) {
        ImageView hand;

        _overlayView = new FrameLayout(MainActivity.this);
        hand = new ImageView(MainActivity.this);
        hand.setImageResource(R.drawable.icon_hand_pointer);
        int w = hand.getDrawable().getIntrinsicWidth();
        int h = hand.getDrawable().getIntrinsicHeight();
        _overlayView.addView(hand);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);

        params.width = w;
        params.height = h;
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = _xOffset;
        params.y = _yOffset;
        _windowManager.addView(_overlayView, params);
    } else {
        // move the view
        params = (WindowManager.LayoutParams) _overlayView.getLayoutParams();
        params.x = _xOffset;
        params.y = _yOffset;
        _windowManager.removeView(_overlayView);
        _overlayView.setLayoutParams(params);
        _windowManager.addView(_overlayView, params);
    }

    _xOffset += 40;
    _yOffset += 100;
}
Run Code Online (Sandbox Code Playgroud)

以下是在应用中使用的此代码的屏幕截图(您可以看到手覆盖;这是在Android 6.0.1的Nexus 5上: 在此输入图像描述