添加了盲盒转盘抽奖功能

修改交友房中开始后,不能点击头像送礼的问题
This commit is contained in:
2025-08-27 19:58:05 +08:00
parent 994d98b515
commit 0b128f3d72
27 changed files with 1333 additions and 137 deletions

View File

@@ -4,13 +4,19 @@ import android.app.Activity;
import com.xscm.moduleutil.activity.IPresenter;
import com.xscm.moduleutil.activity.IView;
import com.xscm.moduleutil.bean.blindboxwheel.BlindBoxBean;
import com.xscm.moduleutil.bean.blindboxwheel.BlindReslutBean;
public class GiftLotteryContacts {
public interface View extends IView<Activity> {
void getGiftListSuccess(BlindBoxBean blindBoxBean);
void drawGiftListSuccess(BlindReslutBean blindReslutBean);
}
public interface IRoomPre extends IPresenter {
void getGiftList(String giftBagId,String roomId);
void drawGiftList(String giftBagId,String gift_user_ids,String roomId,String num);
}
}

View File

@@ -2,7 +2,10 @@ package com.xscm.moduleutil.dialog.giftLottery;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.Window;
@@ -11,10 +14,16 @@ import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.ToastUtils;
import com.xscm.moduleutil.R;
import com.xscm.moduleutil.activity.IPresenter;
import com.xscm.moduleutil.base.BaseMvpDialogFragment;
import com.xscm.moduleutil.bean.GiftBean;
import com.xscm.moduleutil.bean.blindboxwheel.BlindBoxBean;
import com.xscm.moduleutil.bean.blindboxwheel.BlindReslutBean;
import com.xscm.moduleutil.databinding.DialogGiftLotteryBinding;
import com.xscm.moduleutil.http.RetrofitClient;
import com.xscm.moduleutil.widget.GiftCardView;
import java.util.ArrayList;
import java.util.List;
@@ -22,23 +31,51 @@ import java.util.List;
/**
* @author qx
* @data 2025/8/18
* @description: 礼物抽奖
* @description: 天空之境礼物抽奖
*/
public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresenter, DialogGiftLotteryBinding> implements GiftLotteryContacts.View {
private int topCount = 4;
private int bottomCount = 4;
private String giftBagId;
private String roomId;
private String userIds;
///共有12个礼物
private int giftMaxCount = 12;
///最少转6圈
private int minRoundCount = 6;
///距离4个的时候放慢
private int toSlowCount = 4;
private List<GiftCardView> allViewsArray=new ArrayList<>() ;//视图的集合
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 List<GiftBean> giftLists=new ArrayList<>();
@Override
protected GiftLotteryPresenter bindPresenter() {
return new GiftLotteryPresenter(this, getActivity());
}
public static GiftLotteryDialog newInstance(int top, int bottom) {
public static GiftLotteryDialog newInstance(String giftBagId,String roomId,String userIds) {
GiftLotteryDialog dialog = new GiftLotteryDialog();
Bundle args = new Bundle();
args.putInt("top", top);
args.putInt("bottom", bottom);
args.putString("giftBagId", giftBagId);
args.putString("roomId", roomId);
args.putString("userIds", userIds);
dialog.setArguments(args);
return dialog;
}
@@ -53,58 +90,357 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
topCount = getArguments().getInt("top");
bottomCount = getArguments().getInt("bottom");
roomId = getArguments().getString("roomId");
giftBagId = getArguments().getString("giftBagId");
userIds = getArguments().getString("userIds");
}
@Override
protected void initData() {
MvpPre.getGiftList(giftBagId, roomId);
}
private List<Integer> getTargetArrayIndex() {
if (targetArrayIndex == null) {
targetArrayIndex = new ArrayList<>();
}
return targetArrayIndex;
}
private List<Integer> getFinishTargetArrayIndex() {
if (finishTargetArrayIndex == null) {
finishTargetArrayIndex = new ArrayList<>();
}
return finishTargetArrayIndex;
}
@Override
protected void initView() {
mBinding.recyclerGifts.setLayoutManager(new GridLayoutManager(requireContext(), 4));
List<GiftLottery> gifts = new ArrayList<>();
for (int i = 0; i < topCount + bottomCount; i++) {
gifts.add(new GiftLottery(
String.valueOf(i),
"R.mipmap.ic_launcher",
"66666",
"柔情似水",
"66666",
false
));
} GiftLotteryAdapter adapter = new GiftLotteryAdapter(gifts, new GiftLotteryAdapter.OnGiftClickListener() {
allViewsArray.add(mBinding.gift1);
allViewsArray.add(mBinding.gift2);
allViewsArray.add(mBinding.gift3);
allViewsArray.add(mBinding.gift4);
allViewsArray.add(mBinding.gift5);
allViewsArray.add(mBinding.gift6);
allViewsArray.add(mBinding.gift7);
allViewsArray.add(mBinding.gift8);
allViewsArray.add(mBinding.gift9);
allViewsArray.add(mBinding.gift10);
allViewsArray.add(mBinding.gift11);
allViewsArray.add(mBinding.gift12);
mBinding.llOne.setOnClickListener(this::onClisk);
mBinding.llTen.setOnClickListener(this::onClisk);
mBinding.llHundred.setOnClickListener(this::onClisk);
mBinding.swLockYx.setOnClickListener(this::onClisk);
mBinding.swTex.setOnClickListener(this::onClisk);
init(0);
mBinding.swLockYx.setChecked(true);
mBinding.swTex.setChecked(true);
isOpenSpecial=true;
}
private void onClisk(View view) {
int id = view.getId();
if (id == R.id.ll_one){
if (!isDrawing) {
init(1);
startType = 1;
MvpPre.drawGiftList(giftBagId, userIds, roomId, "1");
}else {
com.hjq.toast.ToastUtils.show("正在抽奖中...");
return;
}
}else if (id == R.id.ll_ten){
if (!isDrawing) {
init(2);
startType = 2;
MvpPre.drawGiftList(giftBagId, userIds, roomId, "10");
}else {
com.hjq.toast.ToastUtils.show("正在抽奖中...");
}
}else if (id == R.id.ll_hundred){
if (!isDrawing) {
init(3);
startType = 3;
MvpPre.drawGiftList(giftBagId, userIds, roomId, "100");
}else {
com.hjq.toast.ToastUtils.show("正在抽奖中...");
}
}else if (id == R.id.sw_lock_yx){
isOpenSpecial=mBinding.swLockYx.isChecked();
}else if (id == R.id.sw_tex){
isOpenSpecial=mBinding.swTex.isChecked();
}
}
private void init(int type) {
if (type == 1) {
mBinding.llOne.setBackground(getResources().getDrawable(R.mipmap.chou_x));
mBinding.llTen.setBackground(getResources().getDrawable(R.mipmap.chou_w));
mBinding.llHundred.setBackground(getResources().getDrawable(R.mipmap.chou_w));
} else if (type == 2) {
mBinding.llOne.setBackground(getResources().getDrawable(R.mipmap.chou_w));
mBinding.llTen.setBackground(getResources().getDrawable(R.mipmap.chou_x));
mBinding.llHundred.setBackground(getResources().getDrawable(R.mipmap.chou_w));
} else if (type == 3) {
mBinding.llOne.setBackground(getResources().getDrawable(R.mipmap.chou_w));
mBinding.llTen.setBackground(getResources().getDrawable(R.mipmap.chou_w));
mBinding.llHundred.setBackground(getResources().getDrawable(R.mipmap.chou_x));
}else {
mBinding.llOne.setBackground(getResources().getDrawable(R.mipmap.chou_w));
mBinding.llTen.setBackground(getResources().getDrawable(R.mipmap.chou_w));
mBinding.llHundred.setBackground(getResources().getDrawable(R.mipmap.chou_w));
}
}
///定时器
public void startFastAnimate() {
if (allViewsArray.isEmpty()) {
return;
}
currentGiftCardView = allViewsArray.get(0);
currentGiftCardView.setSelected(true);
// 停止之前的定时器
stopFastAnimate();
if (isOpenSound) {
// 假设此处有播放音乐的逻辑
// player.seekTo(0);
// player.start();
}
roundCount = 0;
delayTime = 0.2;
handler = new Handler(Looper.getMainLooper());
timerRunnable = new Runnable() {
@Override
public void onGiftClick(GiftLottery item) {
}
});
mBinding.recyclerGifts.setAdapter(adapter);
// 绑定按钮点击事件
mBinding.btnLotteryOnce.setOnClickListener(v -> {
adapter.startScanAnimation(); // 启动扫描动画
});
mBinding.btnLotteryTen.setOnClickListener(v -> {
// 抽奖10次每次启动一次扫描
for (int i = 0; i < 10; i++) {
adapter.startScanAnimation();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
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);
}
} else {
currentGiftCardView.setSelected(false);
}
currentIndex = roundCount % giftMaxCount;
GiftCardView giftView = allViewsArray.get(currentIndex);
giftView.setSelected(true);
currentGiftCardView = giftView;
if (roundCount / giftMaxCount == minRoundCount) {
for (int i = 0; i < targetArrayIndex.size(); i++) {
int index = targetArrayIndex.get(i);
if (currentIndex == index) {
GiftCardView targetGiftView = allViewsArray.get(index);
targetGiftView.setSelected(true);
targetGiftView.setVisibilitymResultTextView(true);
targetGiftView.startPulseAnimationWithLayer();
targetArrayIndex.remove(i);
finishTargetArrayIndex.add(index);
break;
}
}
delayTime = 0.03;
if (targetArrayIndex.size()==0) {
isDrawing = false;
stopFastAnimate();
}
} else {
delayTime = delayTime - 0.02;
if (delayTime <= 0.03) {
delayTime = 0.03;
}
}
// 重新调度任务
handler.postDelayed(this, (long) (delayTime * 1000));
}
});
};
// 启动定时器
handler.postDelayed(timerRunnable, (long) (0.2 * 1000));
}
public void stopFastAnimate() {
if (handler != null && timerRunnable != null) {
handler.removeCallbacks(timerRunnable);
timerRunnable=null;
}
}
@Override
protected int getLayoutId() {
return R.layout.dialog_gift_lottery;
}
@Override
public void getGiftListSuccess(BlindBoxBean blindBoxBean) {
if (blindBoxBean != null && blindBoxBean.getGift_list() != null) {
giftLists = blindBoxBean.getGift_list();
// 确保数据数量不超过视图数量
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);
}
}
}
@Override
public void drawGiftListSuccess(BlindReslutBean blindReslutBean) {
if (blindReslutBean!=null || !blindReslutBean.getReslut_list().isEmpty()){
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<12){
GiftCardView giftCardView=allViewsArray.get(j);
GiftBean giftBean=giftLists.get(j);
giftBean.setNumber(reslutList.getCount());
if (!isOpenSpecial){
giftCardView.setVisibilitymResultTextView(false);
giftCardView.setSelected( true);
isDrawing=true;
}
}
}
}
}
if (isOpenSpecial){
startFastAnimate();
}
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
releaseResources();
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
releaseResources();
}
@Override
public void onDestroy() {
super.onDestroy();
releaseResources();
}
/**
* 释放所有资源
*/
private void releaseResources() {
// 清理定时器
if (handler != null && timerRunnable != null) {
handler.removeCallbacksAndMessages(null);
timerRunnable = null;
}
// 停止并清理所有动画
stopAllAnimations();
// 清理GiftCardView中的动画
if (allViewsArray != null) {
for (GiftCardView giftCardView : allViewsArray) {
if (giftCardView != null) {
giftCardView.stopPulseAnimationWithLayer();
}
}
}
// 清理Presenter
if (MvpPre != null) {
// 如果Presenter中有需要清理的资源在这里处理
}
// 清理视图引用
clearViewReferences();
// 建议进行垃圾回收
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;
}
}

View File

@@ -2,12 +2,57 @@ package com.xscm.moduleutil.dialog.giftLottery;
import android.content.Context;
import com.xscm.moduleutil.bean.blindboxwheel.BlindBoxBean;
import com.xscm.moduleutil.bean.blindboxwheel.BlindReslutBean;
import com.xscm.moduleutil.http.BaseObserver;
import com.xscm.moduleutil.presenter.BasePresenter;
import java.lang.ref.WeakReference;
import java.util.List;
import io.reactivex.disposables.Disposable;
public class GiftLotteryPresenter extends BasePresenter<GiftLotteryContacts.View> implements GiftLotteryContacts.IRoomPre {
GiftLotteryContacts.View mView;
public GiftLotteryPresenter(GiftLotteryContacts.View view, Context context) {
super(view, context);
mView = view;
}
@Override
public void getGiftList(String giftBagId, String roomId) {
api.getBoxGiftList(giftBagId, roomId, new BaseObserver<BlindBoxBean>() {
@Override
public void onSubscribe(Disposable d) {
addDisposable( d);
}
@Override
public void onNext(BlindBoxBean blindBoxBeans) {
if (MvpRef==null){
MvpRef=new WeakReference<>(mView);
}
MvpRef.get().getGiftListSuccess(blindBoxBeans);
}
});
}
@Override
public void drawGiftList(String giftBagId, String gift_user_ids, String roomId, String num) {
api.drawGiftList(giftBagId, gift_user_ids, roomId, num, new BaseObserver<BlindReslutBean>() {
@Override
public void onSubscribe(Disposable d) {
addDisposable(d);
}
@Override
public void onNext(BlindReslutBean blindReslutBean) {
if (MvpRef==null){
MvpRef=new WeakReference<>(mView);
}
MvpRef.get().drawGiftListSuccess(blindReslutBean);
}
});
}
}