1:完成天空之境的页面,可以打开规则页面,

This commit is contained in:
2025-08-27 23:12:46 +08:00
parent 0b128f3d72
commit 6a7be63284
4 changed files with 226 additions and 97 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -3,6 +3,8 @@ package com.xscm.moduleutil.dialog.giftLottery;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
@@ -14,6 +16,7 @@ import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.xscm.moduleutil.R;
import com.xscm.moduleutil.activity.IPresenter;
@@ -22,10 +25,14 @@ 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.dialog.WebViewDialog;
import com.xscm.moduleutil.http.RetrofitClient;
import com.xscm.moduleutil.utils.ARouteConstants;
import com.xscm.moduleutil.widget.GiftCardView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
@@ -135,10 +142,12 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
mBinding.llHundred.setOnClickListener(this::onClisk);
mBinding.swLockYx.setOnClickListener(this::onClisk);
mBinding.swTex.setOnClickListener(this::onClisk);
mBinding.tvGz.setOnClickListener(this::onClisk);
init(0);
mBinding.swLockYx.setChecked(true);
mBinding.swTex.setChecked(true);
isOpenSpecial=true;
isOpenSound=true;
}
private void onClisk(View view) {
@@ -169,9 +178,13 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
com.hjq.toast.ToastUtils.show("正在抽奖中...");
}
}else if (id == R.id.sw_lock_yx){
isOpenSpecial=mBinding.swLockYx.isChecked();
isOpenSound=mBinding.swLockYx.isChecked();
}else if (id == R.id.sw_tex){
isOpenSpecial=mBinding.swTex.isChecked();
}else if (id==R.id.tv_gz){
WebViewDialog webViewDialog = new WebViewDialog(getActivity(), getRule_url);
webViewDialog.show();
// ARouter.getInstance().build(ARouteConstants.H5).withString("url", getRule_url).withString("title", "盲盒规则").navigation();
}
}
@@ -207,8 +220,8 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
if (isOpenSound) {
// 假设此处有播放音乐的逻辑
// player.seekTo(0);
// player.start();
playSound("draw_music.mp3");
isOpenSound=false;
}
roundCount = 0;
@@ -244,15 +257,29 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
currentGiftCardView = giftView;
if (roundCount / giftMaxCount == minRoundCount) {
for (int i = 0; i < targetArrayIndex.size(); i++) {
int index = targetArrayIndex.get(i);
// 使用迭代器安全地遍历和修改列表
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();
targetArrayIndex.remove(i);
iterator.remove(); // 安全地移除元素
finishTargetArrayIndex.add(index);
foundTarget = true;
if (!isOpenSound){
if (player != null) {
player.stop();
player.release();
player = null;
}
}
if (!isOpenSpecial){
playSound("draw.mp3");
}
break;
}
}
@@ -260,6 +287,7 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
if (targetArrayIndex.size()==0) {
isDrawing = false;
stopFastAnimate();
return;
}
} else {
delayTime = delayTime - 0.02;
@@ -267,33 +295,71 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
delayTime = 0.03;
}
}
// 重新调度任务
handler.postDelayed(this, (long) (delayTime * 1000));
}
};
// 启动定时器
handler.postDelayed(timerRunnable, (long) (0.2 * 1000));
if (handler != null && timerRunnable != null) {
// 启动定时器
handler.postDelayed(timerRunnable, (long) (0.2 * 1000));
}
}
public void stopFastAnimate() {
isDrawing = false; // 设置标志位
if (handler != null && timerRunnable != null) {
handler.removeCallbacksAndMessages(null); // 移除所有回调和消息
handler = null; // 清空引用
}
// timerRunnable = null;
}
handler.removeCallbacks(timerRunnable);
timerRunnable=null;
private MediaPlayer player;
// 播放音频的方法
private void playSound(String fileName) {
try {
MediaPlayer mediaPlayer = getPlayer(fileName);
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);
}
mediaPlayer.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();
// 如果assets中没有找到文件尝试从raw资源目录加载
// try {
// player = MediaPlayer.create(getContext(), R.raw.draw_music);
// } catch (Exception ex) {
// ex.printStackTrace();
// }
}
}
return player;
}
@Override
protected int getLayoutId() {
return R.layout.dialog_gift_lottery;
}
private String getRule_url;
@Override
public void getGiftListSuccess(BlindBoxBean blindBoxBean) {
if (blindBoxBean != null && blindBoxBean.getGift_list() != null) {
giftLists = blindBoxBean.getGift_list();
getRule_url=blindBoxBean.getRule_url();
// 确保数据数量不超过视图数量
int size = Math.min(giftLists.size(), allViewsArray.size());
@@ -313,27 +379,34 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
@Override
public void drawGiftListSuccess(BlindReslutBean blindReslutBean) {
if (blindReslutBean!=null || !blindReslutBean.getReslut_list().isEmpty()){
for (int i=0;i<blindReslutBean.getReslut_list().size();i++){
if (blindReslutBean != null && blindReslutBean.getReslut_list() != null &&
!blindReslutBean.getReslut_list().isEmpty()) {
// 清空之前的数据
targetArrayIndex.clear();
finishTargetArrayIndex.clear();
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()+"")){
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);
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) {
giftCardView.setVisibilitymResultTextView(true);
giftCardView.setSelected(true);
playSound("draw.mp3");
// 不要设置isDrawing=true这会影响动画控制
}
}
}
}
}
if (isOpenSpecial){
if (isOpenSpecial) {
startFastAnimate();
}
}
@@ -351,6 +424,12 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
releaseResources();
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
releaseResources();
}
@Override
public void onDestroy() {
super.onDestroy();
@@ -362,11 +441,12 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
*/
private void releaseResources() {
// 清理定时器
if (handler != null && timerRunnable != null) {
// 清理定时器
if (handler != null) {
handler.removeCallbacksAndMessages(null);
timerRunnable = null;
handler = null;
}
timerRunnable = null;
// 停止并清理所有动画
stopAllAnimations();
@@ -378,15 +458,16 @@ public class GiftLotteryDialog extends BaseMvpDialogFragment<GiftLotteryPresente
}
}
}
// 清理Presenter
if (MvpPre != null) {
// 如果Presenter中有需要清理的资源在这里处理
}
// 清理视图引用
clearViewReferences();
if (player != null){
player.release();
player = null;
}
// 建议进行垃圾回收
System.gc();
}

View File

@@ -1,12 +1,16 @@
package com.xscm.moduleutil.widget;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.net.Uri;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
@@ -22,6 +26,7 @@ import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.ui.PlayerView;
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.StatusUtil;
import com.liulishuo.okdownload.core.cause.EndCause;
import com.liulishuo.okdownload.core.cause.ResumeFailedCause;
import com.liulishuo.okdownload.core.listener.DownloadListener1;
@@ -32,16 +37,18 @@ import com.opensource.svgaplayer.SVGADynamicEntity;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;
import com.tencent.qgame.animplayer.AnimConfig;
import com.tencent.qgame.animplayer.inter.IAnimListener;
import com.tencent.qgame.animplayer.inter.IFetchResource;
import com.xscm.moduleutil.R;
import com.xscm.moduleutil.databinding.RoomViewSvgaAnimBinding;
import com.xscm.moduleutil.utils.Md5Utils;
import com.xscm.moduleutil.utils.MemoryOptimizationUtils;
import com.xscm.moduleutil.utils.SpUtil;
import com.xscm.moduleutil.utils.logger.Logger;
import com.tencent.qgame.animplayer.AnimConfig;
import com.tencent.qgame.animplayer.inter.IAnimListener;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.LinkedHashMap;
@@ -51,7 +58,6 @@ import java.util.Queue;
public class AvatarFrameView extends FrameLayout implements IAnimListener {
@Override
public void onFailed(int i, @Nullable String s) {
@@ -103,33 +109,9 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
} else {
isPlaying = false;
// playNextFromQueue(); // 播放下一项
// 播放完成后立即清理当前资源
clearCurrentResource();
mainHandler.postDelayed(this::playNextFromQueue, 50);
}
}
private void clearCurrentResource() {
// 清理当前播放的文件引用
// mFile = null;
// 清理SVGA资源
if (svgaSurface != null) {
svgaSurface.stopAnimation();
svgaSurface.clearAnimation();
svgaSurface.setImageDrawable(null);
}
// 清理MP4资源
if (exoPlayer != null) {
exoPlayer.stop();
exoPlayer.clearVideoSurface();
}
if (mBinding != null && mBinding.playView != null) {
mBinding.playView.stopPlay();
}
}
@Override
public void onVideoDestroy() {
@@ -385,52 +367,118 @@ public class AvatarFrameView extends FrameLayout implements IAnimListener {
} else {
DownloadTask task = new DownloadTask.Builder(url, PathUtils.getInternalAppCachePath()
, Md5Utils.getStringMD5(url) + ".mp4")
.setMinIntervalMillisCallbackProcess(100)
.setPassIfAlreadyCompleted(false)
.setMinIntervalMillisCallbackProcess(1000)
.setPassIfAlreadyCompleted(true)
.setAutoCallbackToUIThread(true)
.build();
// if (StatusUtil.isCompleted(task)) {
// playMp4(task.getFile());
// mFile = task.getFile();
// } else {
task.enqueue(new DownloadListener1() {
@Override
public void taskStart(@NonNull DownloadTask task, @NonNull Listener1Assist.Listener1Model model) {
Logger.e("taskStart", model);
}
@Override
public void retry(@NonNull DownloadTask task, @NonNull ResumeFailedCause cause) {
Logger.e("retry", cause);
task.cancel();
// 添加以下优化配置
.setConnectionCount(1) // 对于大文件,减少连接数可能更稳定
.build();
if (StatusUtil.isCompleted(task)) {
playMp4(task.getFile());
mFile = task.getFile();
} else if (StatusUtil.isSameTaskPendingOrRunning(task)) {
// 如果任务正在进行中,等待完成
// 可以通过监听器处理
attachToExistingTask(task);
} else {
task.enqueue(new DownloadListener1() {
@Override
public void taskStart(@NonNull DownloadTask task, @NonNull Listener1Assist.Listener1Model model) {
Logger.e("AvatarFrameView1", model);
}
@Override
public void retry(@NonNull DownloadTask task, @NonNull ResumeFailedCause cause) {
Logger.e("AvatarFrameView2", cause);
task.cancel();
// CrashReport.postCatchedException(new RuntimeException("下载文件重试:" + cause == null ? "" : cause.name()));
}
@Override
public void connected(@NonNull DownloadTask task, int blockCount, long currentOffset, long totalLength) {
Logger.e("connected", blockCount);
}
@Override
public void progress(@NonNull DownloadTask task, long currentOffset, long totalLength) {
Logger.e("progress", currentOffset);
}
@Override
public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull Listener1Assist.Listener1Model model) {
Logger.e("taskEnd", model);
playMp4(task.getFile());
mFile = task.getFile();
if (cause != null && cause != EndCause.COMPLETED) {
// CrashReport.postCatchedException(new RuntimeException("下载任务结束:" + cause == null ? "" : cause.name() + "_realCause:" + realCause == null ? "" : realCause.getMessage()));
}
}
});
// }
@Override
public void connected(@NonNull DownloadTask task, int blockCount, long currentOffset, long totalLength) {
Logger.e("AvatarFrameView3", blockCount);
}
@Override
public void progress(@NonNull DownloadTask task, long currentOffset, long totalLength) {
Logger.e("AvatarFrameView4", currentOffset);
}
@Override
public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull Listener1Assist.Listener1Model model) {
Logger.e("AvatarFrameView5", model);
// playMp4(task.getFile());
// mFile = task.getFile();
// if (cause != null && cause != EndCause.COMPLETED) {
//// CrashReport.postCatchedException(new RuntimeException("下载任务结束:" + cause == null ? "" : cause.name() + "_realCause:" + realCause == null ? "" : realCause.getMessage()));
// }
if (cause == EndCause.COMPLETED) {
File downloadedFile = task.getFile();
if (downloadedFile != null && downloadedFile.exists() && downloadedFile.length() > 0) {
playMp4(downloadedFile);
mFile = downloadedFile;
} else {
Logger.e(TAG, "Downloaded file is invalid");
handleDownloadFailure(url, cause, new IOException("Downloaded file is invalid"));
}
} else {
handleDownloadFailure(url, cause, realCause);
}
}
});
}
}
}
private void handleDownloadFailure(String url, EndCause cause, Exception realCause) {
Logger.e(TAG, "Download failed: " + cause + ", real cause: " + realCause);
// 尝试重试一次
mainHandler.postDelayed(() -> {
// 检查队列是否仍然包含这个项目
boolean shouldRetry = false;
for (PlayItem item : playQueue) {
if (item.url.equals(url)) {
shouldRetry = true;
break;
}
}
if (shouldRetry) {
// 重新添加到队列开头
playQueue.add(new PlayItem(url, mType));
playNextFromQueue();
} else {
isPlaying = false;
playNextFromQueue();
}
}, 1000); // 1秒后重试
}
// 添加检查进行中任务的方法
private void attachToExistingTask(DownloadTask task) {
// 为已经在队列中的任务附加监听器
task.enqueue(new DownloadListener1() {
@Override
public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull Listener1Assist.Listener1Model model) {
if (cause == EndCause.COMPLETED) {
playMp4(task.getFile());
mFile = task.getFile();
} else {
isPlaying = false;
playNextFromQueue();
}
}
// 其他回调方法保持空实现或按需处理
@Override public void taskStart(@NonNull DownloadTask task, @NonNull Listener1Assist.Listener1Model model) {}
@Override public void retry(@NonNull DownloadTask task, @NonNull ResumeFailedCause cause) {}
@Override public void connected(@NonNull DownloadTask task, int blockCount, long currentOffset, long totalLength) {}
@Override public void progress(@NonNull DownloadTask task, long currentOffset, long totalLength) {}
});
}
private void playMp4(File file) {
if (file != null) {
mBinding.playView.startPlay(file);