Overlay screen on Android

前端 未结 1 1756
无人及你
无人及你 2021-02-04 18:49

How is it possible to add Views/Images to my homescreen?

The best example is the facebook messenger: If you long-click at a chat item, you can choose \"pop out cheat hea

1条回答
  •  迷失自我
    2021-02-04 19:10

    Answer is SYSTEM_ALERT_WINDOW, this give you capability to draw over any thing in android, same feature facebook is using for drawing its chat heads.

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

    Sample Code:

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

    0 讨论(0)
提交回复
热议问题