android 获得顶层窗口_Android使用WindowManager实现PopupWindow浮动层窗口

有些时候我们的APP需要在当前屏幕上显示一些提示信息,当然大多数情况下可以通过比如对话框、Activity来完成,但是还是会有一些特殊的场景,比如不能使当前Activity Pause、要始终置于屏幕最前面等,这个时候我们可能就需要使用WindowManager来添加一个浮动层View。浮动层有很多现成的使用场景,比如鼠标、Toast和Dialog都是通过这样的方式实现的,因此你可以在任何时刻弹出一个Toast而不会对当前窗口产生任何影响,更复杂一些的使用场景是系统的音量调节UI、关机对话框等。

看到这里大致了解了浮动层的使用场景,之前的项目中在焦点移动动画、全局Push消息提示中采用了这样的方案,因此使用到了PopupWindow,下面简单介绍一些如何实现。

浮动层的实现主要使用到了WindowManager这个类,我们可以向这个类任意添加一个自定义的View,并且可以控制View从而实现一些交互和动画特效。下面介绍一个简单的PopupWindow的代码实现:

TipsPopupWindow.java

public class TipsPopupWindow {

private FrameLayout popWindowWrap;

private View animationControler;

private View popView;

private Context mContext;

TipsPopupWindow(Context context,WindowManager.LayoutParams popupWindowLayoutParams) {

mContext = context;

popWindowWrap = new FrameLayout(mContext);

LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

popView = layoutInflater.inflate(R.layout.session_tips_prompt, null);

popWindowWrap.setLayoutParams(popupWindowLayoutParams);

popWindowWrap.addView(popView);

WindowManager mWindowManager = (W

你可能感兴趣的:(android,获得顶层窗口)