2025-07-12 19:08:21 +08:00
|
|
|
|
package com.qxcm.moduleutil.service;
|
|
|
|
|
|
|
2025-07-19 14:24:04 +08:00
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
|
import android.animation.ObjectAnimator;
|
2025-07-12 19:08:21 +08:00
|
|
|
|
import android.app.Service;
|
|
|
|
|
|
import android.content.Intent;
|
2025-07-19 14:24:04 +08:00
|
|
|
|
import android.graphics.PixelFormat;
|
|
|
|
|
|
import android.os.Build;
|
2025-07-12 19:08:21 +08:00
|
|
|
|
import android.os.IBinder;
|
2025-07-19 14:24:04 +08:00
|
|
|
|
import android.view.Gravity;
|
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
import android.view.ViewTreeObserver;
|
|
|
|
|
|
import android.view.WindowManager;
|
|
|
|
|
|
import android.widget.TextView;
|
2025-07-12 19:08:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-19 14:24:04 +08:00
|
|
|
|
import com.qxcm.moduleutil.R;
|
|
|
|
|
|
import com.qxcm.moduleutil.event.MqttBean;
|
|
|
|
|
|
import com.qxcm.moduleutil.utils.ImageUtils;
|
2025-07-12 19:08:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-19 14:24:04 +08:00
|
|
|
|
public class EMqttService extends Service {
|
|
|
|
|
|
private WindowManager windowManager;
|
|
|
|
|
|
private View piaoPingView;
|
2025-07-12 19:08:21 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
|
super.onCreate();
|
2025-07-19 14:24:04 +08:00
|
|
|
|
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
2025-07-12 19:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2025-07-19 14:24:04 +08:00
|
|
|
|
if (intent != null && intent.hasExtra("mqttBean")) {
|
|
|
|
|
|
MqttBean mqttBean = (MqttBean) intent.getSerializableExtra("mqttBean");
|
|
|
|
|
|
showPiaoPingMessage(mqttBean);
|
|
|
|
|
|
}
|
|
|
|
|
|
return START_NOT_STICKY;
|
2025-07-12 19:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-07-19 14:24:04 +08:00
|
|
|
|
public void onDestroy() {
|
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
|
if (piaoPingView != null) {
|
|
|
|
|
|
windowManager.removeView(piaoPingView);
|
|
|
|
|
|
}
|
2025-07-12 19:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 14:24:04 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
|
return null;
|
2025-07-12 19:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-19 14:24:04 +08:00
|
|
|
|
public void showPiaoPingMessage(MqttBean mqttBean) {
|
|
|
|
|
|
int screenWidth = getResources().getDisplayMetrics().widthPixels;
|
|
|
|
|
|
int screenHeight = getResources().getDisplayMetrics().heightPixels;
|
|
|
|
|
|
|
|
|
|
|
|
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_SYSTEM_OVERLAY,
|
|
|
|
|
|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
|
|
|
|
|
|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
|
|
|
|
|
|
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
|
|
|
|
|
|
PixelFormat.TRANSLUCENT);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置 Gravity 为左上角
|
|
|
|
|
|
layoutParams.gravity = Gravity.TOP | Gravity.START;
|
|
|
|
|
|
|
|
|
|
|
|
// Y 轴随机位置
|
|
|
|
|
|
layoutParams.y = (int) ((Math.random() * (screenHeight - 200)));
|
|
|
|
|
|
|
|
|
|
|
|
// 初始 X 设为负值,确保 View 在屏幕左侧外
|
|
|
|
|
|
layoutParams.x = -screenWidth;
|
|
|
|
|
|
|
|
|
|
|
|
piaoPingView = LayoutInflater.from(this).inflate(R.layout.item_piaoping, null);
|
|
|
|
|
|
TextView textView = piaoPingView.findViewById(R.id.tv_name);
|
|
|
|
|
|
textView.setText(mqttBean.getList().getText());
|
|
|
|
|
|
ImageUtils.loadHeadCC(mqttBean.getList().getGift_picture(), piaoPingView.findViewById(R.id.iv_piaoping));
|
|
|
|
|
|
TextView tv_time = piaoPingView.findViewById(R.id.tv_num);
|
|
|
|
|
|
tv_time.setText(mqttBean.getList().getNumber());
|
|
|
|
|
|
windowManager.addView(piaoPingView, layoutParams);
|
|
|
|
|
|
|
|
|
|
|
|
piaoPingView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onGlobalLayout() {
|
|
|
|
|
|
piaoPingView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置锚点为左上角,避免偏移干扰
|
|
|
|
|
|
piaoPingView.setPivotX(0);
|
|
|
|
|
|
piaoPingView.setPivotY(0);
|
|
|
|
|
|
|
|
|
|
|
|
// 启动动画:从左外滑入 -> 右外滑出
|
|
|
|
|
|
ObjectAnimator animator = ObjectAnimator.ofFloat(
|
|
|
|
|
|
piaoPingView,
|
|
|
|
|
|
"translationX",
|
|
|
|
|
|
0f, // 初始偏移为 0(此时 View 在左侧外)
|
|
|
|
|
|
screenWidth // 向右移动整个屏幕宽度
|
|
|
|
|
|
);
|
|
|
|
|
|
animator.setDuration(2000); // 整个动画的时长为2秒
|
|
|
|
|
|
|
|
|
|
|
|
// 强制 GPU 渲染
|
|
|
|
|
|
piaoPingView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
|
|
|
|
|
|
|
|
|
|
|
// 延迟显示2秒后开始滑出屏幕的动画
|
|
|
|
|
|
piaoPingView.postDelayed(new Runnable() {
|
2025-07-12 19:08:21 +08:00
|
|
|
|
@Override
|
2025-07-19 14:24:04 +08:00
|
|
|
|
public void run() {
|
|
|
|
|
|
animator.start();
|
2025-07-12 19:08:21 +08:00
|
|
|
|
}
|
2025-07-19 14:24:04 +08:00
|
|
|
|
}, 2000);
|
2025-07-12 19:08:21 +08:00
|
|
|
|
|
2025-07-19 14:24:04 +08:00
|
|
|
|
animator.addListener(new AnimatorListenerAdapter() {
|
2025-07-12 19:08:21 +08:00
|
|
|
|
@Override
|
2025-07-19 14:24:04 +08:00
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
|
windowManager.removeView(piaoPingView);
|
|
|
|
|
|
stopSelf();
|
2025-07-12 19:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-07-19 14:24:04 +08:00
|
|
|
|
});
|
2025-07-12 19:08:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-19 14:24:04 +08:00
|
|
|
|
|