Android 如何叠加十字线

Android 如何叠加十字线,android,overlay,Android,Overlay,我见过许多应用程序,其中用户触摸屏幕,应用程序显示点的重叠十字线视图。我想知道这是如何工作的,以及如何覆盖它,比如在手机的开发者选项中,它覆盖了每个屏幕。谢谢。您可以将布局充气为视图,并将视图添加到根视图中 inflater = LayoutInflater.from(getBaseContext()); View view = inflater.inflate(R.layout.overlay, null); LayoutParams layoutParamsControl= new Layo

我见过许多应用程序,其中用户触摸屏幕,应用程序显示点的重叠十字线视图。我想知道这是如何工作的,以及如何覆盖它,比如在手机的开发者选项中,它覆盖了每个屏幕。谢谢。

您可以将布局充气为视图,并将视图添加到根视图中

inflater = LayoutInflater.from(getBaseContext());
View view = inflater.inflate(R.layout.overlay, null);
LayoutParams layoutParamsControl= new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(view, layoutParamsControl);

如果您想覆盖所有内容,而不仅仅是应用程序,请从服务中调用类似的代码

    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    LayoutInflater layoutInflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mView = layoutInflater.inflate(R.layout.whatToShow, null);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT;
    params.x = 0;
    params.y = 0;
    windowManager.addView(mView, params);
并将其移除

((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
它需要清单中的以下行:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

还可以查看本教程

谢谢。这很有帮助。