1:修改飘屏问题,在BaseActivity中进行监听

2:修改礼物问题
This commit is contained in:
2025-09-06 14:19:23 +08:00
parent 60e2980d89
commit 3b31ba1dea
20 changed files with 616 additions and 153 deletions

View File

@@ -2,6 +2,9 @@ package com.xscm.moduleutil.activity;
import static androidx.core.content.ContextCompat.getSystemService;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -13,11 +16,18 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
@@ -32,10 +42,13 @@ import com.hjq.toast.ToastUtils;
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo;
import com.xscm.moduleutil.R;
import com.xscm.moduleutil.base.CommonAppContext;
import com.xscm.moduleutil.bean.XLHBean;
import com.xscm.moduleutil.event.MqttBean;
import com.xscm.moduleutil.utils.ARouteConstants;
import com.xscm.moduleutil.utils.BackgroundManager;
import com.xscm.moduleutil.utils.ColorManager;
import com.xscm.moduleutil.utils.DisplayUtil;
import com.xscm.moduleutil.utils.ImageUtils;
import com.xscm.moduleutil.utils.LanguageUtil;
import com.xscm.moduleutil.utils.SpUtil;
import com.xscm.moduleutil.widget.PiaoPingManager;
@@ -52,9 +65,14 @@ import java.util.Map;
public abstract class BaseAppCompatActivity<VDB extends ViewDataBinding> extends AppCompatActivity
implements BackgroundManager.BackgroundUpdateListener, ColorManager.ColorChangeListener {
// @Override
// protected void attachBaseContext(Context newBase) {
// super.attachBaseContext(LanguageUtil.attachBaseContext(newBase));
// }
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LanguageUtil.attachBaseContext(newBase));
// 设置字体缩放比例为1.0f,即不跟随系统字体大小变化
super.attachBaseContext(DisplayUtil.attachBaseContext(newBase, 1.0f));
}
protected VDB mBinding;
@@ -82,7 +100,8 @@ public abstract class BaseAppCompatActivity<VDB extends ViewDataBinding> extends
ActivityUtils.finishAllActivities();
}
private PiaoPingManager piaoPingManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -126,22 +145,8 @@ public abstract class BaseAppCompatActivity<VDB extends ViewDataBinding> extends
}
// // 设置全屏模式,隐藏状态栏和导航栏
// View decorView = getWindow().getDecorView();
// decorView.setSystemUiVisibility(
// View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// | View.SYSTEM_UI_FLAG_FULLSCREEN
// | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
// if (piaoPingManager==null){
// piaoPingManager = PiaoPingManager.getInstance(this);
// }
}
// 在Activity中
private static final int REQUEST_OVERLAY_PERMISSION = 1001;
@@ -355,16 +360,398 @@ public abstract class BaseAppCompatActivity<VDB extends ViewDataBinding> extends
return false;
}
// @Subscribe(threadMode = ThreadMode.MAIN)
// public void onMessageReceived(MqttBean mqttBean) {
//// PiaoPingManager.getInstance(this).showPiaoPingMessage(mqttBean);
// FxAppHelper fxAppHelper = FxAppHelper.builder().setContext(this).setLayout(R.layout.item_piaoping).build();
// FloatingX.install(fxAppHelper).show();
// 在类中添加以下成员变量
private final List<MqttBean> messageQueue = new ArrayList<>(); // 消息队列
private boolean isPlaying = false; // 播放状态标志
private final Object queueLock = new Object(); // 队列同步锁
///礼物特效
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageReceived(MqttBean mqttBean) {
if (mqttBean == null) {
return;
}
// 将消息添加到队列
synchronized (queueLock) {
messageQueue.add(mqttBean);
}
// 尝试播放下一个消息
processNextMessage();
}
private void processNextMessage() {
synchronized (queueLock) {
// 如果正在播放或队列为空,则不处理
if (isPlaying || messageQueue.isEmpty()) {
return;
}
// 标记为正在播放
isPlaying = true;
}
MqttBean mqttBean;
synchronized (queueLock) {
mqttBean = messageQueue.remove(0); // 取出队列中的第一个消息
}
// 显示飘屏消息
showFloatingMessage(mqttBean);
}
private View floatingView; // 成员变量保存浮动视图
private boolean isFloatingViewAdded = false; // 标记浮动视图是否已添加到 decorView
private void showFloatingMessage(MqttBean mqttBean) {
ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
// 如果浮动视图未创建,则创建一次
if (floatingView == null) {
floatingView = LayoutInflater.from(this).inflate(R.layout.item_piaoping, null);
// 设置布局参数
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.topMargin = com.sunfusheng.marqueeview.DisplayUtil.dip2px(this, 70);
layoutParams.gravity = android.view.Gravity.TOP | android.view.Gravity.CENTER_HORIZONTAL;
floatingView.setLayoutParams(layoutParams);
// 初始化动画监听器
setupAnimationListener(floatingView, decorView);
}
// 更新视图数据
updateFloatingViewData(mqttBean);
// 如果浮动视图未添加到 decorView则添加
if (!isFloatingViewAdded) {
decorView.addView(floatingView);
isFloatingViewAdded = true;
}
// 重置视图位置并开始动画
resetAndStartAnimation(floatingView);
}
private void setupAnimationListener(View floatingView, ViewGroup decorView) {
// 为视图添加一个全局布局监听器,确保在视图完全加载后再设置动画
floatingView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 移除监听器,避免重复调用
floatingView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// 确保初始位置在屏幕右侧外部
floatingView.setTranslationX(floatingView.getWidth());
// 第一阶段:从右到屏幕右侧边缘(缓慢进入)
ObjectAnimator animator1 = ObjectAnimator.ofFloat(floatingView, "translationX",
floatingView.getWidth(), 0f);
animator1.setDuration(1500); // 延长动画时间到1.5秒
animator1.setInterpolator(new DecelerateInterpolator(2.0f)); // 更平缓的减速效果
animator1.start();
// 第二阶段延迟1秒后从当前位置向左滑出
floatingView.postDelayed(() -> {
ObjectAnimator animator2 = ObjectAnimator.ofFloat(floatingView, "translationX",
0f, -floatingView.getWidth());
animator2.setDuration(1500); // 延长动画时间到1.5秒
animator2.setInterpolator(new DecelerateInterpolator(2.0f)); // 更平缓的减速效果
animator2.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// 动画结束后移除悬浮窗
if (isFloatingViewAdded) {
decorView.removeView(floatingView);
isFloatingViewAdded = false;
}
// 标记播放结束并处理下一个消息
synchronized (queueLock) {
isPlaying = false;
}
processNextMessage();
}
});
animator2.start();
}, 3000); // 停留1秒
}
});
}
private void resetAndStartAnimation(View floatingView) {
// 重置视图位置并开始动画
floatingView.setTranslationX(floatingView.getWidth());
// 第一阶段:从右到屏幕右侧边缘(缓慢进入)
ObjectAnimator animator1 = ObjectAnimator.ofFloat(floatingView, "translationX",
floatingView.getWidth(), 0f);
animator1.setDuration(1500);
animator1.setInterpolator(new DecelerateInterpolator(2.0f));
animator1.start();
// 第二阶段延迟1秒后从当前位置向左滑出
floatingView.postDelayed(() -> {
ObjectAnimator animator2 = ObjectAnimator.ofFloat(floatingView, "translationX",
0f, -floatingView.getWidth());
animator2.setDuration(1500);
animator2.setInterpolator(new DecelerateInterpolator(2.0f));
animator2.start();
}, 3000);
}
private void updateFloatingViewData(MqttBean mqttBean) {
TextView textView = floatingView.findViewById(R.id.tv_name);
TextView textView2 = floatingView.findViewById(R.id.tv_to_name);
TextView tv_time = floatingView.findViewById(R.id.tv_num);
if (mqttBean.getList() != null) {
if (mqttBean.getList().getToUserName() != null) {
textView2.setText("送给" + mqttBean.getList().getToUserName());
} else {
textView2.setText("送给");
}
if (mqttBean.getList().getFromUserName() != null) {
textView.setText(mqttBean.getList().getFromUserName());
} else {
textView.setText("");
}
if (mqttBean.getList().getGift_picture() != null) {
ImageUtils.loadHeadCC(mqttBean.getList().getGift_picture(), floatingView.findViewById(R.id.iv_piaoping));
}
if (mqttBean.getList().getNumber() != null) {
tv_time.setText("x" + mqttBean.getList().getNumber());
} else {
tv_time.setText("x1");
}
} else {
textView2.setText("送给");
textView.setText("");
tv_time.setText("x1");
}
}
// private void showFloatingMessage(MqttBean mqttBean) {
// ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
// View floatingView = LayoutInflater.from(this).inflate(R.layout.item_piaoping, null);
//
// // 设置布局参数使整个布局显示在屏幕顶部并距离顶部100dp
// FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
// FrameLayout.LayoutParams.MATCH_PARENT,
// FrameLayout.LayoutParams.WRAP_CONTENT
// );
// layoutParams.topMargin = com.sunfusheng.marqueeview.DisplayUtil.dip2px(this, 100); // 距离顶部100dp
// layoutParams.gravity = android.view.Gravity.TOP | android.view.Gravity.CENTER_HORIZONTAL; // 顶部居中
// floatingView.setLayoutParams(layoutParams);
//
// TextView textView = floatingView.findViewById(R.id.tv_name);
// TextView textView2 = floatingView.findViewById(R.id.tv_to_name);
// TextView tv_time = floatingView.findViewById(R.id.tv_num);
//
// // 添加对 getList() 返回值的空值检查
// if (mqttBean.getList() != null) {
// // 检查各个字段是否为 null
// if (mqttBean.getList().getToUserName() != null) {
// textView2.setText("送给" + mqttBean.getList().getToUserName());
// } else {
// textView2.setText("送给");
// }
//
// if (mqttBean.getList().getFromUserName() != null) {
// textView.setText(mqttBean.getList().getFromUserName());
// } else {
// textView.setText("");
// }
//
// // 检查礼物图片
// if (mqttBean.getList().getGift_picture() != null) {
// ImageUtils.loadHeadCC(mqttBean.getList().getGift_picture(), floatingView.findViewById(R.id.iv_piaoping));
// }
//
// // 检查数量
// if (mqttBean.getList().getNumber() != null) {
// tv_time.setText("x" + mqttBean.getList().getNumber());
// } else {
// tv_time.setText("x1");
// }
// } else {
// // 如果 getList() 返回 null设置默认值
// textView2.setText("送给");
// textView.setText("");
// tv_time.setText("x1");
// }
//
// floatingView.setTranslationX(10000); // 先放到屏幕外
//
// floatingView.post(() -> {
// // 确保初始位置在屏幕右侧外部
// floatingView.setTranslationX(floatingView.getWidth());
//
// // 第一阶段:从右到屏幕右侧边缘(缓慢进入)
// ObjectAnimator animator1 = ObjectAnimator.ofFloat(floatingView, "translationX",
// floatingView.getWidth(), 0f);
// animator1.setDuration(1500); // 延长动画时间到1.5秒
// animator1.setInterpolator(new DecelerateInterpolator(2.0f)); // 更平缓的减速效果
// animator1.start();
//
// // 第二阶段延迟1秒后从当前位置向左滑出
// floatingView.postDelayed(() -> {
// ObjectAnimator animator2 = ObjectAnimator.ofFloat(floatingView, "translationX",
// 0f, -floatingView.getWidth());
// animator2.setDuration(1500); // 延长动画时间到1.5秒
// animator2.setInterpolator(new DecelerateInterpolator(2.0f)); // 更平缓的减速效果
// animator2.addListener(new AnimatorListenerAdapter() {
// @Override
// public void onAnimationEnd(Animator animation) {
// // 动画结束后移除悬浮窗
// decorView.removeView(floatingView);
//
// // 标记播放结束并处理下一个消息
// synchronized (queueLock) {
// isPlaying = false;
// }
// processNextMessage();
// }
// });
// animator2.start();
// }, 3000); // 停留1秒
// });
// decorView.addView(floatingView);
// }
// 在类中添加新的成员变量
private View xlhFloatingView; // XLH类型的浮动视图
private boolean isXlhFloatingViewAdded = false; // 标记XLH浮动视图是否已添加到 decorView
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(XLHBean event) {
showPiaoPingMessageXlh(event);
}
private void showPiaoPingMessageXlh(XLHBean event) {
// 创建 FloatingX 配置
ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
// 如果XLH浮动视图未创建则创建一次
if (xlhFloatingView == null) {
xlhFloatingView = LayoutInflater.from(this).inflate(R.layout.item_piaoping_xlh, null);
// 设置布局参数
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.topMargin = com.sunfusheng.marqueeview.DisplayUtil.dip2px(this, 100);
layoutParams.gravity = android.view.Gravity.TOP | android.view.Gravity.CENTER_HORIZONTAL;
xlhFloatingView.setLayoutParams(layoutParams);
// 初始化动画监听器
setupXlhAnimationListener(xlhFloatingView, decorView);
} // 更新视图数据
updateXlhFloatingViewData(event);
// 如果浮动视图未添加到 decorView则添加
if (!isXlhFloatingViewAdded) {
decorView.addView(xlhFloatingView);
isXlhFloatingViewAdded = true;
}
// 重置视图位置并开始动画
resetAndStartXlhAnimation(xlhFloatingView);
}
private void setupXlhAnimationListener(View floatingView, ViewGroup decorView) {
// 为视图添加一个全局布局监听器,确保在视图完全加载后再设置动画
floatingView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 移除监听器,避免重复调用
floatingView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// 确保初始位置在屏幕右侧外部
floatingView.setTranslationX(floatingView.getWidth());
// 第一阶段:从右到屏幕右侧边缘(缓慢进入)
ObjectAnimator animator1 = ObjectAnimator.ofFloat(floatingView, "translationX",
floatingView.getWidth(), 0f);
animator1.setDuration(1500); // 延长动画时间到1.5秒
animator1.setInterpolator(new DecelerateInterpolator(2.0f)); // 更平缓的减速效果
animator1.start();
// 第二阶段延迟1秒后从当前位置向左滑出
floatingView.postDelayed(() -> {
ObjectAnimator animator2 = ObjectAnimator.ofFloat(floatingView, "translationX",
0f, -floatingView.getWidth());
animator2.setDuration(1500); // 延长动画时间到1.5秒
animator2.setInterpolator(new DecelerateInterpolator(2.0f)); // 更平缓的减速效果
animator2.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// 动画结束后移除悬浮窗
if (isXlhFloatingViewAdded) {
decorView.removeView(floatingView);
isXlhFloatingViewAdded = false;
}
// 标记播放结束并处理下一个消息
synchronized (queueLock) {
isPlaying = false;
}
processNextMessage();
}
});
animator2.start();
}, 3000); // 停留1秒
}
});
}
private void resetAndStartXlhAnimation(View floatingView) {
// 重置视图位置并开始动画
floatingView.setTranslationX(floatingView.getWidth());
// 第一阶段:从右到屏幕右侧边缘(缓慢进入)
ObjectAnimator animator1 = ObjectAnimator.ofFloat(floatingView, "translationX",
floatingView.getWidth(), 0f);
animator1.setDuration(1500);
animator1.setInterpolator(new DecelerateInterpolator(2.0f));
animator1.start();
// 第二阶段延迟1秒后从当前位置向左滑出
floatingView.postDelayed(() -> {
ObjectAnimator animator2 = ObjectAnimator.ofFloat(floatingView, "translationX",
0f, -floatingView.getWidth());
animator2.setDuration(1500);
animator2.setInterpolator(new DecelerateInterpolator(2.0f));
animator2.start();
}, 3000);
}
private void updateXlhFloatingViewData(XLHBean xlhBean) {
TextView textView = xlhFloatingView.findViewById(R.id.tv_name);
ImageView xlh_image = xlhFloatingView.findViewById(R.id.im_xlh);
if (xlhBean!=null){
xlh_image.setImageDrawable(xlhBean.getFrom_type() == 1 ? getResources().getDrawable(R.mipmap.xlh_jjks) : getResources().getDrawable(R.mipmap.xlh_zsks));
textView.setText(xlhBean.getText());
} else {
textView.setText("");
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(ChatInfo event) {
String id = event.getId().replace("g", "");
ARouter.getInstance().build(ARouteConstants.H5).withString("url", CommonAppContext.getInstance().getCurrentEnvironment().getH5Url() + "/web/index.html#/pages/union/setGroup?id=" + SpUtil.getToken() + "&guildId=" + id).navigation();
}
}