在Android上叠加屏幕

时间:2013-12-18 12:41:16

标签: android overlay homescreen

如何将视图/图像添加到我的主屏幕?

最好的例子是facebook messenger:如果你长时间点击聊天项目,你可以选择“弹出作弊头”,然后你的屏幕上会出现一个小按钮。

还有像屏幕破碎机这样的应用程序,来自破碎显示屏的图像覆盖了手机上的所有内容。

我已经搜索过了,但我不知道它叫什么。

我希望你们能够理解我的英语。

1 个答案:

答案 0 :(得分:9)

答案是SYSTEM_ALERT_WINDOW,这使你能够在android中绘制任何东西,同样的功能facebook用于绘制聊天头。

http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW

示例代码:

    private WindowManager windowManager;
    private ImageView imageView;
   // Get window manager reference

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    imageView= new ImageView(YOUR_CONTEXT_HERE);
    imageView.setImageResource(R.drawable.android_head);

    // Setup layout parameter
    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.TOP | Gravity.LEFT; // Orientation
    params.x = 100; // where you want to draw this, coordinates
    params.y = 100;
    // At it to window manager for display, it will be printed over any thing
    windowManager.addView(chatHead, params);


   // Make sure to remove it when you are done, else it will stick there until you reboot
   // Do keep track of same reference of view you added, don't mess with that
   windowManager.removeView(imageView);