Files
yusheng-android/MainModule/src/main/java/com/xscm/modulemain/dialog/GiftLotteryDialog.java
2025-11-07 09:22:39 +08:00

1114 lines
45 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.modulemain.dialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetFileDescriptor;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import com.blankj.utilcode.util.GsonUtils;
import com.xscm.modulemain.R;
import com.xscm.modulemain.databinding.DialogGiftLotteryBinding;
import com.xscm.moduleutil.base.BaseMvpDialogFragment;
import com.xscm.moduleutil.bean.GiftBean;
import com.xscm.moduleutil.bean.MqttXlhEnd;
import com.xscm.moduleutil.bean.WalletBean;
import com.xscm.moduleutil.bean.XLHBean;
import com.xscm.moduleutil.bean.blindboxwheel.BlindBoxBean;
import com.xscm.moduleutil.bean.blindboxwheel.BlindReslutBean;
import com.xscm.moduleutil.bean.blindboxwheel.XlhDrawBean;
import com.xscm.moduleutil.dialog.RechargeDialogFragment;
import com.xscm.moduleutil.dialog.giftLottery.GiftLotteryContacts;
import com.xscm.moduleutil.dialog.giftLottery.GiftLotteryDialogFragment;
import com.xscm.moduleutil.dialog.giftLottery.GiftLotteryPresenter;
import com.xscm.moduleutil.dialog.giftLottery.PrizePoolDialog;
import com.xscm.moduleutil.enumType.LotteryEvent;
import com.xscm.moduleutil.widget.CircularProgressView;
import com.xscm.moduleutil.widget.GiftCardView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author qx
* @data 2025/8/18
* @description: 天空之境礼物抽奖
*/
public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresenter, DialogGiftLotteryBinding> implements GiftLotteryContacts.View {
private String giftBagId;
private String roomId;
private String userIds;
/// 共有12个礼物
private int giftMaxCount;
/// 最少转6圈
private int minRoundCount;
/// 距离4个的时候放慢
private int toSlowCount = 4;
private List<GiftCardView> allViewsArray;//视图的集合
private int targetIndex;//目标的下标
private List<Integer> targetArrayIndex = new ArrayList<>();//中奖的下标
private List<Integer> finishTargetArrayIndex = new ArrayList<>();
private int currentIndex;
/// 当前转到第几个下标
private int roundCount;//转动多少次
private double delayTime;//延时
private GiftCardView currentGiftCardView;
private boolean isDrawing;//是否正在抽奖
private boolean isOpenSound;//是否开启音效
private boolean isOpenSpecial;//是否开启特效
private Handler handler;
private Runnable timerRunnable;//定时器
private int startType;//类型点击的事抽奖1次还是10次还是100次
private CircularProgressView circularProgress;
private List<GiftBean> giftLists = new ArrayList<>();
private LotteryEvent userIdType; // 新增枚举类型字段
private int type;//10:对应天空之境 11对应岁月之城 12对应时空之巅
private GiftLotteryDialogFragment giftLotteryDialogFragment;
private String blind_box_turntable_id = "";//本次抽奖标识id
private BlindBoxBean.XlhData xlhData;
private int icon;//金币金额
private String heart_id = "";
private String auction_id="";
@Override
protected GiftLotteryPresenter bindPresenter() {
return new GiftLotteryPresenter(this, getActivity());
}
public static GiftLotteryDialog newInstance(String giftBagId, String roomId, String userIds, String heart_id,String auction_id) {
GiftLotteryDialog dialog = new GiftLotteryDialog();
Bundle args = new Bundle();
args.putString("giftBagId", giftBagId);
args.putString("roomId", roomId);
args.putString("userIds", userIds);
args.putString("heart_id", heart_id);
args.putString("auction_id", auction_id);
dialog.setArguments(args);
return dialog;
}
@Nullable
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
return dialog;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
roomId = getArguments().getString("roomId");
giftBagId = getArguments().getString("giftBagId");
userIds = getArguments().getString("userIds");
// 根据userIds确定类型
userIdType = LotteryEvent.fromLotteryEvent(giftBagId);
heart_id = getArguments().getString("heart_id");
auction_id = getArguments().getString("auction_id");
}
@Override
protected void initData() {
MvpPre.getGiftList(giftBagId, roomId);
MvpPre.wallet();
mBinding.mirroeSky.swLockYx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean check) {
isOpenSound = check;//音效
if (!check) {
stopPlay();
}
}
});
mBinding.cityTime.swLockYx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean check) {
isOpenSound = check;//音效
if (!check) {
stopPlay();
}
}
});
mBinding.pinnacleTime.swLockYx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean check) {
isOpenSound = check;//音效
if (!check) {
stopPlay();
}
}
});
mBinding.mirroeSky.swTex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean check) {
isOpenSpecial = check;
}
});
mBinding.cityTime.swTex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean check) {
isOpenSpecial = check;
}
});
mBinding.pinnacleTime.swTex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean check) {
isOpenSpecial = check;
}
});
}
private void stopPlay() {
if ( player != null) {
player.stop();
player.reset();
player = null;
// mediaPlayer.stop();
// mediaPlayer.reset();
// mediaPlayer = null;
}
}
@Override
protected void initView() {
// 根据不同的用户类型显示不同的界面
setupUIBasedOnUserType();
}
@Override
protected void initDialogStyle(Window window) {
super.initDialogStyle(window);
window.setGravity(Gravity.BOTTOM);
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
if (window != null) {
// 获取屏幕高度
android.util.DisplayMetrics displayMetrics = new android.util.DisplayMetrics();
requireActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenHeight = displayMetrics.heightPixels;
// 设置高度为屏幕高度的100%(全屏)
int heightInPx = (int) (screenHeight * 0.79);
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawableResource(android.R.color.transparent);
// 可选:设置动画样式(从底部弹出)
window.setWindowAnimations(com.xscm.moduleutil.R.style.CommonShowDialogBottom);
}
}
private void setupUIBasedOnUserType() {
switch (userIdType) {
case MIRROR_SKY://天空之境
setupSingleUserUI();
type = 10;
break;
case CITY_TIME://岁月之巅
setupMultiUserUI();
type = 11;
break;
case PINNACLE_TIME://时空之巅
setupAllUsersUI();
type = 12;
break;
}
}
/// 天空之境
private void setupSingleUserUI() {
allViewsArray = new ArrayList<>();
mBinding.mirroeSky.getRoot().setVisibility(View.VISIBLE);
giftMaxCount = 12;
minRoundCount = 6;
allViewsArray.add(mBinding.mirroeSky.gift1);
allViewsArray.add(mBinding.mirroeSky.gift2);
allViewsArray.add(mBinding.mirroeSky.gift3);
allViewsArray.add(mBinding.mirroeSky.gift4);
allViewsArray.add(mBinding.mirroeSky.gift5);
allViewsArray.add(mBinding.mirroeSky.gift6);
allViewsArray.add(mBinding.mirroeSky.gift7);
allViewsArray.add(mBinding.mirroeSky.gift8);
allViewsArray.add(mBinding.mirroeSky.gift9);
allViewsArray.add(mBinding.mirroeSky.gift10);
allViewsArray.add(mBinding.mirroeSky.gift11);
allViewsArray.add(mBinding.mirroeSky.gift12);
mBinding.mirroeSky.llOne.setOnClickListener(this::onClisk);
mBinding.mirroeSky.llTen.setOnClickListener(this::onClisk);
mBinding.mirroeSky.llHundred.setOnClickListener(this::onClisk);
mBinding.mirroeSky.swLockYx.setOnClickListener(this::onClisk);
mBinding.mirroeSky.swTex.setOnClickListener(this::onClisk);
mBinding.mirroeSky.tvGz.setOnClickListener(this::onClisk);
mBinding.mirroeSky.tvJc.setOnClickListener(this::onClisk);
mBinding.mirroeSky.tvJl.setOnClickListener(this::onClisk);
mBinding.mirroeSky.tvStart.setOnClickListener(this::onClisk);
mBinding.mirroeSky.exchangeLayout.setOnClickListener(this::onClisk);
init(0);
mBinding.mirroeSky.swLockYx.setChecked(true);
mBinding.mirroeSky.swTex.setChecked(true);
isOpenSpecial = true;
isOpenSound = true;
circularProgress = mBinding.mirroeSky.circularProgressView;
circularProgress.setProgress(0);
}
//岁月之城
private void setupMultiUserUI() {
allViewsArray = new ArrayList<>();
mBinding.cityTime.getRoot().setVisibility(View.VISIBLE);
giftMaxCount = 10;
minRoundCount = 7;
allViewsArray.add(mBinding.cityTime.gift1);
allViewsArray.add(mBinding.cityTime.gift2);
allViewsArray.add(mBinding.cityTime.gift3);
allViewsArray.add(mBinding.cityTime.gift4);
allViewsArray.add(mBinding.cityTime.gift5);
allViewsArray.add(mBinding.cityTime.gift6);
allViewsArray.add(mBinding.cityTime.gift7);
allViewsArray.add(mBinding.cityTime.gift8);
allViewsArray.add(mBinding.cityTime.gift9);
allViewsArray.add(mBinding.cityTime.gift10);
mBinding.cityTime.llOne.setOnClickListener(this::onClisk);
mBinding.cityTime.llTen.setOnClickListener(this::onClisk);
mBinding.cityTime.llHundred.setOnClickListener(this::onClisk);
mBinding.cityTime.swLockYx.setOnClickListener(this::onClisk);
mBinding.cityTime.swTex.setOnClickListener(this::onClisk);
mBinding.cityTime.tvGz.setOnClickListener(this::onClisk);
mBinding.cityTime.tvJc.setOnClickListener(this::onClisk);
mBinding.cityTime.tvJl.setOnClickListener(this::onClisk);
mBinding.cityTime.tvStart.setOnClickListener(this::onClisk);
mBinding.cityTime.exchangeLayout.setOnClickListener(this::onClisk);
init2(0);
mBinding.cityTime.swLockYx.setChecked(true);
mBinding.cityTime.swTex.setChecked(true);
isOpenSpecial = true;
isOpenSound = true;
circularProgress = mBinding.cityTime.circularProgressView;
circularProgress.setProgress(0);
}
//时空之巅
private void setupAllUsersUI() {
allViewsArray = new ArrayList<>();
mBinding.pinnacleTime.getRoot().setVisibility(View.VISIBLE);
giftMaxCount = 10;
minRoundCount = 7;
allViewsArray.add(mBinding.pinnacleTime.gift1);
allViewsArray.add(mBinding.pinnacleTime.gift2);
allViewsArray.add(mBinding.pinnacleTime.gift3);
allViewsArray.add(mBinding.pinnacleTime.gift4);
allViewsArray.add(mBinding.pinnacleTime.gift5);
allViewsArray.add(mBinding.pinnacleTime.gift6);
allViewsArray.add(mBinding.pinnacleTime.gift7);
allViewsArray.add(mBinding.pinnacleTime.gift8);
allViewsArray.add(mBinding.pinnacleTime.gift9);
allViewsArray.add(mBinding.pinnacleTime.gift10);
mBinding.pinnacleTime.llOne.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.llTen.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.llHundred.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.swLockYx.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.swTex.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.tvGz.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.tvJc.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.tvJl.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.tvStart.setOnClickListener(this::onClisk);
mBinding.pinnacleTime.exchangeLayout.setOnClickListener(this::onClisk);
init3(0);
mBinding.pinnacleTime.swLockYx.setChecked(true);
mBinding.pinnacleTime.swTex.setChecked(true);
isOpenSpecial = true;
isOpenSound = true;
circularProgress = mBinding.pinnacleTime.circularProgressView;
circularProgress.setProgress(0);
}
private void onClisk(View view) {
int id = view.getId();
if (id == R.id.ll_one) {
if (!isDrawing) {
isDrawing = true;
// init(1);
startType = 1;
MvpPre.drawGiftList(giftBagId, userIds, roomId, "1", heart_id,auction_id);
} else {
// com.hjq.toast.ToastUtils.show("正在抽奖中...");
return;
}
} else if (id == R.id.ll_ten) {
if (!isDrawing) {
isDrawing = true;
// init(2);
startType = 2;
MvpPre.drawGiftList(giftBagId, userIds, roomId, "6", heart_id,auction_id);
} else {
// com.hjq.toast.ToastUtils.show("正在抽奖中...");
}
} else if (id == R.id.ll_hundred) {//抽奖100次
if (!isDrawing) {
isDrawing = true;
// init(3);
startType = 3;
MvpPre.drawGiftList(giftBagId, userIds, roomId, "9", heart_id,auction_id);
} else {
// com.hjq.toast.ToastUtils.show("正在抽奖中...");
}
} else if (id == R.id.tv_gz) {//规则
Bundle bundle = new Bundle();
bundle.putString("url", getRule_url);
bundle.putInt("type", type);
WebViewDialog dialog = new WebViewDialog(getActivity(), bundle);
dialog.show();
// WebViewDialog webViewDialog = new WebViewDialog(getActivity(), getRule_url);
// webViewDialog.show();
} else if (id == R.id.tv_jc) {
if (giftLists != null && !giftLists.isEmpty()) {
PrizePoolDialog prizePoolDialog = new PrizePoolDialog(getActivity());
prizePoolDialog.updateData(giftLists, type);
prizePoolDialog.show();
} else {
com.hjq.toast.ToastUtils.show("奖池数据加载中,请稍后再试");
}
} else if (id == R.id.tv_jl) {
// 如果当前dialog存在且正在显示先关闭
if (giftLotteryDialogFragment != null && giftLotteryDialogFragment.isVisible()) {
giftLotteryDialogFragment.dismiss();
}
giftLotteryDialogFragment = GiftLotteryDialogFragment.newInstance(giftBagId);
giftLotteryDialogFragment.show(getChildFragmentManager(), "GiftLotteryDialogFragment");
} else if (id == R.id.tv_start) {
if (xlhData != null) {
if (xlhData.getStatus() == 1) {
FragmentManager fm = getParentFragmentManager();
if (fm != null && !fm.isDestroyed()) {
TourClubDialogFragment newDialog = TourClubDialogFragment.newInstance(
roomId);
newDialog.show(fm, "TourClubDialogFragment");
}
this.dismissAllowingStateLoss();
}
}
} else if (id == R.id.exchange_layout) {
// ARouter.getInstance().build(ARouteConstants.CURRENCY).navigation();
RechargeDialogFragment.show(roomId, null, getActivity().getSupportFragmentManager(),"0","0");
}
}
private void setBackground() {
// 预加载资源
Drawable drawableX = ContextCompat.getDrawable(getContext(), com.xscm.moduleutil.R.mipmap.chou_x);
Drawable drawableW = ContextCompat.getDrawable(getContext(), com.xscm.moduleutil.R.mipmap.chou_w);
if (icon > 0 && box_price > 0) {
if (type == 10) {
updateBackground(mBinding.mirroeSky.llOne, icon >= box_price, drawableX, drawableW);
updateBackground(mBinding.mirroeSky.llTen, icon >= box_price * 6, drawableX, drawableW);
updateBackground(mBinding.mirroeSky.llHundred, icon >= box_price * 9, drawableX, drawableW);
} else if (type == 11) {
updateBackground(mBinding.cityTime.llOne, icon >= box_price, drawableX, drawableW);
updateBackground(mBinding.cityTime.llTen, icon >= box_price * 6, drawableX, drawableW);
updateBackground(mBinding.cityTime.llHundred, icon > box_price * 9, drawableX, drawableW);
} else if (type == 12) {
updateBackground(mBinding.pinnacleTime.llOne, icon >= box_price, drawableX, drawableW);
updateBackground(mBinding.pinnacleTime.llTen, icon >= box_price * 6, drawableX, drawableW);
updateBackground(mBinding.pinnacleTime.llHundred, icon >= box_price * 9, drawableX, drawableW);
} else {
// 兜底处理:未知 type 时全部设为不可点击 + 默认背景
setAllBackgroundToDefault(drawableW);
}
} else {
// icon 或 box_price <= 0 时全部置灰不可点击
setAllBackgroundToDefault(drawableW);
}
}
private void updateBackground(View view, boolean clickable, Drawable drawableX, Drawable drawableW) {
if (clickable) {
view.setBackground(drawableX);
view.setClickable(true);
} else {
view.setBackground(drawableW);
view.setClickable(false);
}
}
private void setAllBackgroundToDefault(Drawable defaultDrawable) {
switch (type) {
case 10:
mBinding.mirroeSky.llOne.setBackground(defaultDrawable);
mBinding.mirroeSky.llTen.setBackground(defaultDrawable);
mBinding.mirroeSky.llHundred.setBackground(defaultDrawable);
mBinding.mirroeSky.llOne.setClickable(false);
mBinding.mirroeSky.llTen.setClickable(false);
mBinding.mirroeSky.llHundred.setClickable(false);
break;
case 11:
mBinding.cityTime.llOne.setBackground(defaultDrawable);
mBinding.cityTime.llTen.setBackground(defaultDrawable);
mBinding.cityTime.llHundred.setBackground(defaultDrawable);
mBinding.cityTime.llOne.setClickable(false);
mBinding.cityTime.llTen.setClickable(false);
mBinding.cityTime.llHundred.setClickable(false);
break;
case 12:
mBinding.pinnacleTime.llOne.setBackground(defaultDrawable);
mBinding.pinnacleTime.llTen.setBackground(defaultDrawable);
mBinding.pinnacleTime.llHundred.setBackground(defaultDrawable);
mBinding.pinnacleTime.llOne.setClickable(false);
mBinding.pinnacleTime.llTen.setClickable(false);
mBinding.pinnacleTime.llHundred.setClickable(false);
break;
default:
// 忽略未知类型
break;
}
}
private void init(int type) {
if (type == 1) {
mBinding.mirroeSky.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
mBinding.mirroeSky.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.mirroeSky.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
} else if (type == 2) {
mBinding.mirroeSky.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.mirroeSky.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
mBinding.mirroeSky.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
} else if (type == 3) {
mBinding.mirroeSky.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.mirroeSky.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.mirroeSky.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
} else {
mBinding.mirroeSky.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.mirroeSky.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.mirroeSky.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
}
}
private void init2(int type) {
if (type == 1) {
mBinding.cityTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
mBinding.cityTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.cityTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
} else if (type == 2) {
mBinding.cityTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.cityTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
mBinding.cityTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
} else if (type == 3) {
mBinding.cityTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.cityTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.cityTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
} else {
mBinding.cityTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.cityTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.cityTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
}
}
private void init3(int type) {
if (type == 1) {
mBinding.pinnacleTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
mBinding.pinnacleTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.pinnacleTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
} else if (type == 2) {
mBinding.pinnacleTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.pinnacleTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
mBinding.pinnacleTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
} else if (type == 3) {
mBinding.pinnacleTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.pinnacleTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.pinnacleTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_x));
} else {
mBinding.pinnacleTime.llOne.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.pinnacleTime.llTen.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
mBinding.pinnacleTime.llHundred.setBackground(getResources().getDrawable(com.xscm.moduleutil.R.mipmap.chou_w));
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageReceived(MqttXlhEnd event) {
XLHBean xlhBean = GsonUtils.fromJson(event.getMessage(), XLHBean.class);
if (xlhBean.getFrom_type()==100 || xlhBean.getFrom_type()==101 || xlhBean.getFrom_type()==102){
UpView(xlhBean.getXlh_data());
}else if (xlhBean.getFrom_type()==104){
xlhData.setCurrent_num(0);
xlhData.setStatus(0);
UpView(xlhData);
}
if (xlhBean.getFrom_type() == 3) {
xlhData.setCurrent_num(0);
xlhData.setStatus(0);
UpView(xlhData);
}
}
/// 定时器
public void startFastAnimate() {
if (allViewsArray.isEmpty()) {
return;
}
for (GiftCardView gridView : allViewsArray) {
gridView.setSelected(false);
gridView.stopPulseAnimationWithLayer();
gridView.setVisibilitymResultTextView(false);
}
currentGiftCardView = allViewsArray.get(0);
currentGiftCardView.setSelected(true);
// 停止之前的定时器
stopFastAnimate();
stopPlay();
Log.e("isOpenSound===", "isOpenSound111" + isOpenSound);
if (isOpenSound) {
// 假设此处有播放音乐的逻辑
Log.e("isOpenSound===", "raw_music.mp");
playSound("draw_music.mp3");
// isOpenSound=false;
}
roundCount = 0;
delayTime = 0.22;
handler = new Handler(Looper.getMainLooper());
timerRunnable = new Runnable() {
@Override
public void run() {
roundCount++;
if ((roundCount / giftMaxCount == minRoundCount) && startType != 0) {
boolean has = false;
for (int index : finishTargetArrayIndex) {
if (index == currentIndex) {
has = true;
break;
}
}
currentGiftCardView.setSelected(false);
GiftCardView giftView = allViewsArray.get(currentIndex);
if (has) {
giftView.setSelected(true);
} else {
giftView.setSelected(false);
giftView.stopPulseAnimationWithLayer();
}
} else {
currentGiftCardView.setSelected(false);
currentGiftCardView.stopPulseAnimationWithLayer();
}
currentIndex = roundCount % giftMaxCount;
GiftCardView giftView = allViewsArray.get(currentIndex);
giftView.setSelected(true);
currentGiftCardView = giftView;
if (roundCount / giftMaxCount == minRoundCount) {
// 使用迭代器安全地遍历和修改列表
boolean foundTarget = false;
Iterator<Integer> iterator = targetArrayIndex.iterator();
while (iterator.hasNext()) {
int index = iterator.next();
if (currentIndex == index) {
GiftCardView targetGiftView = allViewsArray.get(index);
targetGiftView.setSelected(true);
targetGiftView.setVisibilitymResultTextView(true);
targetGiftView.startPulseAnimationWithLayer();
iterator.remove(); // 安全地移除元素
finishTargetArrayIndex.add(index);
foundTarget = true;
stopPlay();
break;
}
}
delayTime = 0.03;
if (targetArrayIndex.isEmpty()) {
MvpPre.giftSend(blind_box_turntable_id);
MvpPre.wallet();
isDrawing = false;
playSound("xuanz.mp3");
stopFastAnimate();
return;
}
} else {
delayTime = delayTime - 0.02;
if (delayTime <= 0.03) {
delayTime = 0.03;
}
}
// 重新调度任务
handler.postDelayed(this, (long) (delayTime * 1000));
}
};
if (handler != null) {
// 启动定时器
handler.postDelayed(timerRunnable, (long) (0.2 * 1000));
}
}
public void stopFastAnimate() {
if (handler != null) {
// 移除特定的 Runnable更精确
if (timerRunnable != null) {
handler.removeCallbacks(timerRunnable);
}
// 或者移除所有回调(更彻底)
handler.removeCallbacksAndMessages(null);
handler = null;
}
timerRunnable = null;
}
private MediaPlayer player;
// MediaPlayer mediaPlayer;
// 播放音频的方法
private void playSound(String fileName) {
try {
player = getPlayer(fileName);
if (player != null) {
if (player.isPlaying()) {
player.seekTo(0);
}
player.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private MediaPlayer getPlayer(String fileName) {
if (player == null) {
try {
// 从assets目录加载音频文件
AssetFileDescriptor afd = getContext().getAssets().openFd(fileName);
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
// else {
// try {
// AssetFileDescriptor afd = getContext().getAssets().openFd(fileName);
// mediaPlayer = new MediaPlayer();
// mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
// mediaPlayer.prepare();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
return player;
}
@Override
protected int getLayoutId() {
return R.layout.dialog_gift_lottery;
}
private String getRule_url;
private int box_price;//服务器返回的单次抽奖价格
@Override
public void getGiftListSuccess(BlindBoxBean blindBoxBean) {
if (blindBoxBean != null && blindBoxBean.getGift_list() != null) {
box_price = blindBoxBean.getBox_price();
upTitle(blindBoxBean.getBox_price());
giftLists = blindBoxBean.getGift_list();
getRule_url = blindBoxBean.getRule_url();
// 确保数据数量不超过视图数量
int size = Math.min(giftLists.size(), allViewsArray.size());
// 一对一绑定数据到视图
for (int i = 0; i < size; i++) {
GiftCardView giftCardView = allViewsArray.get(i);
GiftBean giftBean = giftLists.get(i);
giftCardView.bindGiftData(giftBean);
}
// 如果礼物数量少于视图数量,隐藏多余的视图
for (int i = size; i < allViewsArray.size(); i++) {
allViewsArray.get(i).setVisibility(View.GONE);
}
if (blindBoxBean.getIs_xlh() == 1) {
BlindBoxBean.XlhData xlhData = blindBoxBean.getXlhDataAsObject();
UpView(xlhData);
} else {
if (type == 10) {
mBinding.mirroeSky.llProgress.setVisibility(View.INVISIBLE);
mBinding.mirroeSky.tvStart.setVisibility(View.INVISIBLE);
} else if (type == 11) {
mBinding.cityTime.llProgress.setVisibility(View.INVISIBLE);
mBinding.cityTime.tvStart.setVisibility(View.INVISIBLE);
} else if (type == 12) {
mBinding.pinnacleTime.llProgress.setVisibility(View.INVISIBLE);
mBinding.pinnacleTime.tvStart.setVisibility(View.INVISIBLE);
}
}
}
setBackground();
}
private void upTitle(int boxPrice) {
if (type == 10) {
mBinding.mirroeSky.oneTitle.setText(boxPrice + "币一次");
mBinding.mirroeSky.tenTitle.setText((boxPrice * 6) + "币六次");
mBinding.mirroeSky.hundredTitle.setText((boxPrice * 9) + "币九次");
} else if (type == 11) {
mBinding.cityTime.oneTitle.setText(boxPrice + "币一次");
mBinding.cityTime.tenTitle.setText((boxPrice * 6) + "币六次");
mBinding.cityTime.hundredTitle.setText((boxPrice * 9) + "币九次");
} else if (type == 12) {
mBinding.pinnacleTime.oneTitle.setText(boxPrice + "币一次");
mBinding.pinnacleTime.tenTitle.setText((boxPrice * 6) + "币六次");
mBinding.pinnacleTime.hundredTitle.setText((boxPrice * 9) + "币九次");
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageReceived(BlindBoxBean.XlhData xlhData) {
UpView(xlhData);
}
public void UpView(BlindBoxBean.XlhData xlhData) {
if (xlhData != null) {
this.xlhData = xlhData;
int currentNum = xlhData.getCurrent_num();
int startNum = Integer.parseInt(xlhData.getStart_num());
int waitingStartNum = Integer.parseInt(xlhData.getWaiting_start_num());
int progress = 0;
String progressText = "";
if (currentNum >= startNum) {
// 已经达到开启条件,显示完整进度或特殊状态
progress = 100;
progressText = "100%";
} else {
// 还未达到等待开始条件计算相对于waiting_start_num的进度
progress = (int) (((float) currentNum / startNum) * 100);
progressText = progress + "%";
}
if (giftBagId.equals("10")) {
circularProgress.setProgress(progress * 10);
mBinding.mirroeSky.tvProgress.setText(progressText);
} else if (giftBagId.equals("11")) {
circularProgress.setProgress(progress * 10);
mBinding.cityTime.tvProgress.setText(progressText);
} else if (giftBagId.equals("12")) {
circularProgress.setProgress(progress * 10);
mBinding.pinnacleTime.tvProgress.setText(progressText);
}
if (xlhData.getStatus() == 0) {
mBinding.mirroeSky.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.dengt);
mBinding.cityTime.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.dengt);
mBinding.pinnacleTime.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.dengt);
} else if (xlhData.getStatus() == 1) {
mBinding.mirroeSky.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.zhensgh);
mBinding.cityTime.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.zhensgh);
mBinding.pinnacleTime.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.zhensgh);
} else if (xlhData.getStatus() == 2) {
mBinding.mirroeSky.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.jijang);
mBinding.cityTime.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.jijang);
mBinding.pinnacleTime.tvStart.setBackgroundResource(com.xscm.moduleutil.R.mipmap.jijang);
}
}
}
@Override
public void drawGiftListSuccess(BlindReslutBean blindReslutBean) {
if (blindReslutBean != null && blindReslutBean.getReslut_list() != null &&
!blindReslutBean.getReslut_list().isEmpty()) {
for (GiftCardView gridView : allViewsArray) {
gridView.setSelected(false);
gridView.stopPulseAnimationWithLayer();
gridView.setVisibilitymResultTextView(false);
}
// 清空之前的数据
targetArrayIndex.clear();
finishTargetArrayIndex.clear();
if (blindReslutBean.getBlind_box_turntable_id() != null) {
blind_box_turntable_id = blindReslutBean.getBlind_box_turntable_id();
}
if (allViewsArray.size() > 0) {
for (int i = 0; i < blindReslutBean.getReslut_list().size(); i++) {
BlindReslutBean.ReslutList reslutList = blindReslutBean.getReslut_list().get(i);
for (int j = 0; j < giftLists.size(); j++) {
if (giftLists.get(j).getGift_id().equals(reslutList.getGift_id() + "")) {
targetArrayIndex.add(j);
if (j < giftMaxCount) {
GiftCardView giftCardView = allViewsArray.get(j);
GiftBean giftBean = giftLists.get(j);
giftBean.setCount(reslutList.getCount());
giftCardView.setVisibilitymResultTextView(true);
giftCardView.setSelected(true);
// 不要设置isDrawing=true这会影响动画控制
// MvpPre.wallet();
// isDrawing = false;
// MvpPre.giftSend(blind_box_turntable_id);
giftCardView.bindGiftData(giftBean);
}
}
}
}
}
if (MvpPre == null) {
MvpPre = new GiftLotteryPresenter(this, getActivity());
}
MvpPre.wallet();
MvpPre.giftSend(blind_box_turntable_id);
Log.e("isOpenSound===", "isOpenSound1222" + isOpenSound);
if (isOpenSpecial) {//特效
Log.e("isOpenSound===", "isOpenSound333" + isOpenSound);
startFastAnimate();
} else {
if (isOpenSound) {//音效
playSound("xuanz.mp3");
}
isDrawing = false;
}
} else {
isDrawing = false;
}
}
@Override
public void getMyRecordSuccess(List<GiftBean> data) {
}
@Override
public void getAllRecordSuccess(List<GiftBean> data) {
}
@Override
public void finishRefreshLoadMore() {
}
@Override
public void wallet(WalletBean walletBean) {
if (walletBean != null) {
icon = (int) Double.parseDouble(
(walletBean.getCoin() != null && !walletBean.getCoin().isEmpty())
? walletBean.getCoin()
: "0"
);
if (isAdded() && getActivity() != null && !getActivity().isFinishing() &&
getDialog() != null && getDialog().isShowing()) {
if (type == 10) {
mBinding.mirroeSky.tvIcon.setText(walletBean.getCoin());
} else if (type == 11) {
mBinding.cityTime.tvIcon.setText(walletBean.getCoin());
} else if (type == 12) {
mBinding.pinnacleTime.tvIcon.setText(walletBean.getCoin());
}
} else {
// dialog 已关闭,不进行 UI 更新
return;
}
} else {
// walletBean 为空时也关闭 dialog
if (getActivity() != null && !getActivity().isFinishing()) {
dismiss();
}
}
setBackground();
}
@Override
public void xlhChouSuccess(List<XlhDrawBean> data) {
}
// TODO: 2025/8/29 接收im推送过来的消息
// @Subscribe(threadMode = ThreadMode.MAIN)
// public void onMusicPlay(RoomMessageEvent message) {
// if (message.getMsgType() == 1056) {
// UpView(message.getText().getXlh_data());
// }
// }
@Override
public void onDestroyView() {
super.onDestroyView();
releaseResources();
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
// releaseResources();
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
// releaseResources();
}
@Override
public void onDestroy() {
super.onDestroy();
// releaseResources();
}
/**
* 释放所有资源
*/
private void releaseResources() {
// 清理定时器
// 清理定时器
if (handler != null) {
handler.removeCallbacksAndMessages(null);
handler = null;
}
timerRunnable = null;
// 停止并清理所有动画
stopAllAnimations();
// 清理GiftCardView中的动画
if (allViewsArray != null) {
for (GiftCardView giftCardView : allViewsArray) {
if (giftCardView != null) {
giftCardView.stopPulseAnimationWithLayer();
}
}
}
// 清理Presenter
if (MvpPre != null) {
// 如果Presenter中有需要清理的资源在这里处理
}
// 清理视图引用
clearViewReferences();
if (player != null) {
player.release();
player = null;
}
// 建议进行垃圾回收
System.gc();
}
/**
* 停止所有动画
*/
private void stopAllAnimations() {
// 停止所有GiftCardView动画
if (allViewsArray != null) {
for (GiftCardView giftCardView : allViewsArray) {
if (giftCardView != null) {
giftCardView.stopPulseAnimationWithLayer();
}
}
}
}
/**
* 清理视图引用
*/
private void clearViewReferences() {
// 清理GiftCardView列表
if (allViewsArray != null) {
allViewsArray.clear();
}
// 清理Presenter引用
if (MvpPre != null) {
MvpPre.detachView();
MvpPre = null;
}
// 清理Binding引用
if (mBinding != null) {
// 清理Binding中的视图引用
mBinding = null;
}
}
/**
* 停止抽奖过程
*/
private void stopLotteryProcess() {
// 停止当前的抽奖动画
if (handler != null && timerRunnable != null) {
handler.removeCallbacks(timerRunnable);
}
// 重置抽奖状态
isDrawing = false;
currentIndex = 0;
roundCount = 0;
}
}