如何在 Android 中制作类似 Facebook Messenger 的通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20015971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:09:50  来源:igfitidea点击:

How to make a Facebook Messenger-like notification like this in Android

android

提问by MSaudi

I want to implement notifications like the one in the following image.

我想实现如下图所示的通知。

Notification appears any time. I think it's of course a background service waiting for new messages from the server then shows this. What I think this is an activity implemented as dialog with this custom UI. Am I correct? And is it a normal startActivity method from the service? And how do I do the transition animation to make it appears slowly from left to right with zooming when show up?

通知随时出现。我认为这当然是一个后台服务,等待来自服务器的新消息然后显示这一点。我认为这是一个作为带有此自定义 UI 的对话框实现的活动。我对么?它是服务的正常 startActivity 方法吗?以及如何进行过渡动画以使其在显示时从左到右缓慢显示并进行缩放?

Enter image description here

在此处输入图片说明

回答by Apoorv

Check out this link http://www.piwai.info/chatheads-basics. He provides information about how to add them on your screen.

查看此链接http://www.piwai.info/chatheads-basics。他提供了有关如何将它们添加到屏幕上的信息。

The trick is to add a Viewto the WindowManagerlike following code

诀窍是ViewWindowManager下面的代码中添加一个

private WindowManager windowManager;
private ImageView chatHead;

public void addView()
{
  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);
}

Don't forget to add the permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

不要忘记添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>