如何覆盖十字准线

时间:2013-12-19 07:40:16

标签: android overlay

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

2 个答案:

答案 0 :(得分:1)

您可以inflate布局作为视图,add view作为根视图。

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);

答案 1 :(得分:1)

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

    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" />

另请参阅本教程 http://www.piwai.info/chatheads-basics/