101 lines
3.6 KiB
Java
101 lines
3.6 KiB
Java
|
|
package com.qxcm.moduleutil.widget;
|
||
|
|
|
||
|
|
import static com.liulishuo.okdownload.OkDownloadProvider.context;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.graphics.PixelFormat;
|
||
|
|
import android.os.Build;
|
||
|
|
import android.view.Gravity;
|
||
|
|
import android.view.LayoutInflater;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.WindowManager;
|
||
|
|
import android.widget.TextView;
|
||
|
|
|
||
|
|
import com.qxcm.moduleutil.R;
|
||
|
|
import com.qxcm.moduleutil.event.MqttBean;
|
||
|
|
import com.qxcm.moduleutil.utils.ImageUtils;
|
||
|
|
|
||
|
|
import org.greenrobot.eventbus.EventBus;
|
||
|
|
import org.greenrobot.eventbus.Subscribe;
|
||
|
|
import org.greenrobot.eventbus.ThreadMode;
|
||
|
|
/**
|
||
|
|
* @Author
|
||
|
|
* @Time 2025/7/18 21:52
|
||
|
|
* @Description 飘屏管理器
|
||
|
|
*/
|
||
|
|
public class PiaoPingManager {
|
||
|
|
private static PiaoPingManager instance;
|
||
|
|
private WindowManager windowManager;
|
||
|
|
private View piaoPingView;
|
||
|
|
private boolean isPiaoPingShown = false;
|
||
|
|
|
||
|
|
private PiaoPingManager(Context context) {
|
||
|
|
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||
|
|
if (windowManager == null) return;
|
||
|
|
|
||
|
|
// 创建飘屏消息的布局
|
||
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
||
|
|
piaoPingView = inflater.inflate(R.layout.item_piaoping, null);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static synchronized PiaoPingManager getInstance(Context context) {
|
||
|
|
if (instance == null) {
|
||
|
|
instance = new PiaoPingManager(context.getApplicationContext());
|
||
|
|
}
|
||
|
|
return instance;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void showPiaoPingMessage(MqttBean mqttBean) {
|
||
|
|
if (isPiaoPingShown || piaoPingView == null || windowManager == null) return;
|
||
|
|
|
||
|
|
// 设置布局参数
|
||
|
|
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
|
||
|
|
WindowManager.LayoutParams.MATCH_PARENT,
|
||
|
|
WindowManager.LayoutParams.WRAP_CONTENT,
|
||
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
|
||
|
|
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
|
||
|
|
WindowManager.LayoutParams.TYPE_PHONE,
|
||
|
|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
||
|
|
PixelFormat.TRANSLUCENT);
|
||
|
|
|
||
|
|
layoutParams.gravity = Gravity.TOP;
|
||
|
|
layoutParams.y = 100; // 设置垂直位置
|
||
|
|
|
||
|
|
// 设置消息内容
|
||
|
|
View piaoPingView = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.item_piaoping, null);
|
||
|
|
TextView textView = piaoPingView.findViewById(R.id.tv_name);
|
||
|
|
TextView textView2 = piaoPingView.findViewById(R.id.tv_to_name);
|
||
|
|
textView2.setText("送给"+mqttBean.getList().getToUserName());
|
||
|
|
textView.setText(mqttBean.getList().getFromUserName());
|
||
|
|
ImageUtils.loadHeadCC(mqttBean.getList().getGift_picture(), piaoPingView.findViewById(R.id.iv_piaoping));
|
||
|
|
TextView tv_time = piaoPingView.findViewById(R.id.tv_num);
|
||
|
|
tv_time.setText("x"+mqttBean.getList().getNumber());
|
||
|
|
|
||
|
|
// 添加到窗口管理器
|
||
|
|
windowManager.addView(piaoPingView, layoutParams);
|
||
|
|
isPiaoPingShown = true;
|
||
|
|
|
||
|
|
// 设置定时任务,一段时间后移除飘屏消息
|
||
|
|
piaoPingView.postDelayed(() -> {
|
||
|
|
if (piaoPingView != null) {
|
||
|
|
windowManager.removeView(piaoPingView);
|
||
|
|
isPiaoPingShown = false;
|
||
|
|
}
|
||
|
|
}, 3000); // 3秒后移除
|
||
|
|
}
|
||
|
|
|
||
|
|
public void subscribe() {
|
||
|
|
EventBus.getDefault().register(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void unsubscribe() {
|
||
|
|
EventBus.getDefault().unregister(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||
|
|
public void onMessageReceived(MqttBean mqttBean) {
|
||
|
|
showPiaoPingMessage(mqttBean);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|