274 lines
9.2 KiB
Java
274 lines
9.2 KiB
Java
package com.xscm.moduleutil.widget;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.graphics.Color;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.animation.Animation;
|
|
import android.view.animation.TranslateAnimation;
|
|
import android.widget.ImageView;
|
|
import android.widget.TextView;
|
|
|
|
import com.xscm.moduleutil.R;
|
|
import com.xscm.moduleutil.event.MqttBean;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class QXGiftDriftView extends ViewGroup {
|
|
|
|
private static QXGiftDriftView instance;
|
|
|
|
private ImageView bgImageView;
|
|
private TextView titleLabel;
|
|
private ImageView giftImageView;
|
|
private TextView countLabel;
|
|
private boolean isPlaying=false;
|
|
private boolean isClose;
|
|
|
|
private List<MqttBean.ListBean> dataArray = new ArrayList<>();
|
|
private MqttBean.ListBean model;
|
|
|
|
private Context context;
|
|
private int screenWidth;
|
|
|
|
public static QXGiftDriftView getInstance(Context context) {
|
|
if (instance == null) {
|
|
instance = new QXGiftDriftView(context);
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
private QXGiftDriftView(Context context) {
|
|
super(context);
|
|
this.context = context;
|
|
|
|
// 获取屏幕宽度
|
|
screenWidth = context.getResources().getDisplayMetrics().widthPixels;
|
|
|
|
// 初始化视图
|
|
initSubviews();
|
|
|
|
// 从SharedPreferences读取设置
|
|
SharedPreferences prefs = context.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);
|
|
isClose = prefs.getBoolean("kIsCloseDrifPop", false);
|
|
}
|
|
|
|
private void initSubviews() {
|
|
// 设置视图尺寸
|
|
int width = scaleWidth(316);
|
|
int height = scaleWidth(50);
|
|
setLayoutParams(new LayoutParams(width, height));
|
|
|
|
// 背景图片
|
|
bgImageView = new ImageView(context);
|
|
bgImageView.setImageResource(R.mipmap.gift_p_b);
|
|
addView(bgImageView);
|
|
|
|
// 标题标签
|
|
titleLabel = new TextView(context);
|
|
titleLabel.setTextSize(14);
|
|
titleLabel.setTextColor(Color.WHITE);
|
|
addView(titleLabel);
|
|
|
|
// 礼物图片
|
|
giftImageView = new ImageView(context);
|
|
giftImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
|
addView(giftImageView);
|
|
|
|
// 数量标签
|
|
countLabel = new TextView(context);
|
|
countLabel.setTextSize(14);
|
|
countLabel.setTextColor(Color.WHITE);
|
|
addView(countLabel);
|
|
}
|
|
|
|
@Override
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
// 布局子视图
|
|
int width = getWidth();
|
|
int height = getHeight();
|
|
|
|
// 背景图片填满整个视图
|
|
bgImageView.layout(0, 0, width, height);
|
|
|
|
// 标题标签居中偏左
|
|
int titleWidth = titleLabel.getMeasuredWidth();
|
|
int titleHeight = titleLabel.getMeasuredHeight();
|
|
int titleLeft = (width - titleWidth) / 2 - scaleWidth(30);
|
|
int titleTop = (height - titleHeight) / 2;
|
|
titleLabel.layout(titleLeft, titleTop, titleLeft + titleWidth, titleTop + titleHeight);
|
|
|
|
// 礼物图片在标题右侧
|
|
int giftSize = scaleWidth(20);
|
|
int giftLeft = titleLeft + titleWidth + scaleWidth(5);
|
|
int giftTop = titleTop + (titleHeight - giftSize) / 2;
|
|
giftImageView.layout(giftLeft, giftTop, giftLeft + giftSize, giftTop + giftSize);
|
|
|
|
// 数量标签在礼物图片右侧
|
|
int countWidth = countLabel.getMeasuredWidth();
|
|
int countLeft = giftLeft + giftSize;
|
|
countLabel.layout(countLeft, titleTop, countLeft + countWidth, titleTop + titleHeight);
|
|
}
|
|
|
|
@Override
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
int width = scaleWidth(316);
|
|
int height = scaleWidth(50);
|
|
setMeasuredDimension(width, height);
|
|
|
|
// 测量子视图
|
|
measureChildren(widthMeasureSpec, heightMeasureSpec);
|
|
}
|
|
|
|
public void addGiftModel(MqttBean.ListBean model) {
|
|
if (isClose) {
|
|
return;
|
|
}
|
|
dataArray.add(model);
|
|
giftAction();
|
|
}
|
|
|
|
public void addGiftModelList(List<MqttBean.ListBean> list) {
|
|
dataArray.addAll(list);
|
|
giftAction();
|
|
}
|
|
|
|
private void giftAction() {
|
|
if (isPlaying) {
|
|
return;
|
|
}
|
|
|
|
if (dataArray.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
isPlaying = true;
|
|
model = dataArray.get(0);
|
|
|
|
// 添加到窗口(这里需要根据实际情况获取根布局)
|
|
ViewGroup rootView = (ViewGroup) ((Activity) context).getWindow().getDecorView();
|
|
rootView.addView(QXGiftDriftView.getInstance( context));
|
|
|
|
// 设置初始位置(屏幕右侧外)
|
|
setX(screenWidth);
|
|
|
|
// 进入动画
|
|
TranslateAnimation enterAnim = new TranslateAnimation(
|
|
Animation.ABSOLUTE, screenWidth,
|
|
Animation.ABSOLUTE, (screenWidth - scaleWidth(316)) / 2,
|
|
Animation.ABSOLUTE, 0,
|
|
Animation.ABSOLUTE, 0
|
|
);
|
|
enterAnim.setDuration(1500);
|
|
enterAnim.setAnimationListener(new Animation.AnimationListener() {
|
|
@Override
|
|
public void onAnimationStart(Animation animation) {}
|
|
|
|
@Override
|
|
public void onAnimationEnd(Animation animation) {
|
|
// 停留后退出
|
|
postDelayed(() -> {
|
|
TranslateAnimation exitAnim = new TranslateAnimation(
|
|
Animation.ABSOLUTE, (screenWidth - scaleWidth(316)) / 2,
|
|
Animation.ABSOLUTE, -screenWidth,
|
|
Animation.ABSOLUTE, 0,
|
|
Animation.ABSOLUTE, 0
|
|
);
|
|
exitAnim.setDuration(2000);
|
|
exitAnim.setAnimationListener(new Animation.AnimationListener() {
|
|
@Override
|
|
public void onAnimationStart(Animation animation) {}
|
|
|
|
@Override
|
|
public void onAnimationEnd(Animation animation) {
|
|
// 移除视图并处理下一个
|
|
ViewGroup rootView = (ViewGroup) getParent();
|
|
if (rootView != null) {
|
|
rootView.removeView(QXGiftDriftView.getInstance( context));
|
|
}
|
|
|
|
if (!dataArray.isEmpty()) {
|
|
dataArray.remove(0);
|
|
}
|
|
isPlaying = false;
|
|
|
|
if (!dataArray.isEmpty()) {
|
|
giftAction();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationRepeat(Animation animation) {}
|
|
});
|
|
startAnimation(exitAnim);
|
|
}, 1000); // 停留1秒
|
|
}
|
|
|
|
@Override
|
|
public void onAnimationRepeat(Animation animation) {}
|
|
});
|
|
startAnimation(enterAnim);
|
|
}
|
|
|
|
public void setModel(MqttBean.ListBean model) {
|
|
this.model = model;
|
|
|
|
String text = String.format("%s送给%s", model.getFromUserName(), model.getToUserName());
|
|
// 这里需要使用SpannableString来实现富文本效果
|
|
// 简化处理,直接设置文本
|
|
titleLabel.setText(text);
|
|
|
|
// 加载图片 - 使用图片加载库如Glide
|
|
// Glide.with(context).load(model.getGiftPicture()).into(giftImageView);
|
|
|
|
countLabel.setText(String.format("X%s", model.getNumber()));
|
|
}
|
|
|
|
public void drifPopIsClose(boolean isClose) {
|
|
this.isClose = isClose;
|
|
setVisibility(isClose ? View.GONE : View.VISIBLE);
|
|
|
|
SharedPreferences prefs = context.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);
|
|
prefs.edit().putBoolean("kIsCloseDrifPop", isClose).apply();
|
|
|
|
if (isClose) {
|
|
ViewGroup rootView = (ViewGroup) getParent();
|
|
if (rootView != null) {
|
|
rootView.removeView(this);
|
|
}
|
|
dataArray.clear();
|
|
isPlaying = false;
|
|
}
|
|
}
|
|
|
|
private int scaleWidth(int width) {
|
|
// 根据屏幕密度进行缩放
|
|
float density = context.getResources().getDisplayMetrics().density;
|
|
return (int) (width * density);
|
|
}
|
|
|
|
// QXGiftScrollModel 类(需要单独定义)
|
|
public static class QXGiftScrollModel {
|
|
private String fromUserName;
|
|
private String toUserName;
|
|
private String giftPicture;
|
|
private String number;
|
|
|
|
// getters and setters
|
|
public String getFromUserName() { return fromUserName; }
|
|
public void setFromUserName(String fromUserName) { this.fromUserName = fromUserName; }
|
|
|
|
public String getToUserName() { return toUserName; }
|
|
public void setToUserName(String toUserName) { this.toUserName = toUserName; }
|
|
|
|
public String getGiftPicture() { return giftPicture; }
|
|
public void setGiftPicture(String giftPicture) { this.giftPicture = giftPicture; }
|
|
|
|
public String getNumber() { return number; }
|
|
public void setNumber(String number) { this.number = number; }
|
|
}
|
|
}
|