初步完成弹窗方案

This commit is contained in:
董宏宇
2021-08-04 19:52:28 +08:00
parent c2abe34d00
commit bb7470e157
4 changed files with 65 additions and 10 deletions

View File

@@ -1,10 +1,12 @@
package com.mogo.module.warning.notification;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import android.view.WindowManager;
import com.mogo.module.warning.R;
@@ -14,16 +16,42 @@ import com.mogo.module.warning.R;
* 通知管理
*/
public class WarningNotificationManager {
// 这里采用添加View的方式,配合动画完成弹窗的效果,仅限应用内部弹窗,伴随着Activity后台也会隐藏
private static WindowManager mWindowManager;
private static WindowManager.LayoutParams mLayoutParams;
private static View mView;
public static void show(Activity activity) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.notification_v2x_msg_vr, null);
Toast toast = new Toast(activity.getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
public static void show(Context context) {
// 获取 WindowManager
mWindowManager = (WindowManager) context.getSystemService(Service.WINDOW_SERVICE);
mLayoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
0, 0,
PixelFormat.TRANSPARENT);
// flag 设置 Window 属性,悬浮窗的行为,比如说不可聚焦,非模态对话框等等
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// type 设置 Window 类别(层级),悬浮窗的类型一般设为2002表示在所有应用程序之上但在状态栏之下
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
// 悬浮窗的对齐方式
mLayoutParams.gravity = Gravity.TOP;
//
mLayoutParams.format = PixelFormat.RGBA_8888;
// 悬浮窗X的位置
mLayoutParams.x = 0;
//悬浮窗Y的位置此时需要减去statusBar的高度
mLayoutParams.y = 110;
//添加动画
mLayoutParams.windowAnimations = R.style.notice_dialog_anim_bottom2top;
if (mView != null) {
mWindowManager.removeView(mView);
}
mView = LayoutInflater.from(context).inflate(R.layout.notification_v2x_msg_vr, null, false);
mWindowManager.addView(mView, mLayoutParams);
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="-100%"
android:toXDelta="0"
android:interpolator="@android:anim/overshoot_interpolator"
android:toYDelta="0">
</translate>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/anticipate_interpolator"
android:toXDelta="0"
android:toYDelta="-100%">
</translate>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="notice_dialog_anim_bottom2top">
<item name="android:windowEnterAnimation">@anim/notice_dialog_anim_top2bottom_in</item>
<item name="android:windowExitAnimation">@anim/notice_dialog_anim_top2bottom_out</item>
</style>
</resources>