哪些 API 用于绘制其他应用程序(如 Facebook 的聊天头)?

问题描述:

Facebook 如何在 Android 上创建聊天头?在所有其他视图之上创建浮动视图的 API 是什么?

How does Facebook create the Chat Heads on Android? What is the API to create the floating views on top of all other views?

这个:

允许应用程序使用类型打开窗口TYPE_SYSTEM_ALERT,显示在所有其他应用程序之上.很少有应用程序应该使用此权限;这些窗口旨在用于与用户进行系统级交互.

Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. Very few applications should use this permission; these windows are intended for system-level interaction with the user.

常量值:android.permission.SYSTEM_ALERT_WINDOW"

Constant Value: "android.permission.SYSTEM_ALERT_WINDOW"

//完整代码这里:

public class ChatHeadService extends Service {

  private WindowManager windowManager;
  private ImageView chatHead;

  @Override public IBinder onBind(Intent intent) {
    // Not used
    return null;
  }

  @Override public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.android_head);

    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;
    params.x = 0;
    params.y = 100;

    windowManager.addView(chatHead, params);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    if (chatHead != null) windowManager.removeView(chatHead);
  }
}

不要忘记以某种方式启动服务:

Don't forget to start the service somehow:

startService(new Intent(context, ChatHeadService.class));

.. 并将此服务添加到您的清单中.

.. And add this service to your Manifest.