Android: 如何不管启动了多少个activity都在上面加一层?

Android: How to add a layer on top no matter how many activities started?

我想播放一个 gif 动画,这个视图每秒都会在屏幕上移动,但是如果我开始一个 activity 我需要再次添加这个视图并且位置会改变。

见下图。我需要把它贴在每个视图上。有什么办法吗?

将此视图设置到 WindowManager 中,它将始终显示在顶部

为您应用中的所有活动创建一个 BaseActivity 以显示叠加视图或布局。 你可以在其他 Activity

上继承 BaseActivity

您可以添加 gif 支持的视图或布局 例如,我在 showOverlay 方法中添加了 overlay_layout。 你可以在任何你想显示的地方调用 showOverlay 方法,你可以使用 removeOverlay 有条件地删除。 请注意 showOverlay 和 removeOverlay 应该在 BaseActivity

void showOverlay(){
LayoutInflater inflater = activity.getLayoutInflater();
layout = inflater.inflate(R.layout.overlay_layout, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.BOTTOM;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION;
final WindowManager mWindowManager = (WindowManager);
activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            mWindowManager.addView(layout, params);
}
void removeOverlay(){
windowManager.removeView(view);
}