|
@@ -1,24 +1,79 @@
|
|
|
package com.fujica.abk.utils;
|
|
package com.fujica.abk.utils;
|
|
|
|
|
|
|
|
-import ohos.agp.components.DirectionalLayout;
|
|
|
|
|
|
|
+import com.amap.adapter.view.ViewGroup;
|
|
|
|
|
+import com.fujica.abk.ResourceTable;
|
|
|
|
|
+import ohos.agp.components.Component;
|
|
|
|
|
+import ohos.agp.components.ComponentContainer;
|
|
|
|
|
+import ohos.agp.components.LayoutScatter;
|
|
|
import ohos.agp.components.ProgressBar;
|
|
import ohos.agp.components.ProgressBar;
|
|
|
import ohos.agp.components.Text;
|
|
import ohos.agp.components.Text;
|
|
|
-import ohos.agp.components.element.ShapeElement;
|
|
|
|
|
import ohos.agp.colors.RgbColor;
|
|
import ohos.agp.colors.RgbColor;
|
|
|
|
|
+import ohos.agp.components.element.ShapeElement;
|
|
|
import ohos.agp.utils.Color;
|
|
import ohos.agp.utils.Color;
|
|
|
-import ohos.agp.utils.LayoutAlignment;
|
|
|
|
|
-import ohos.agp.utils.Rect;
|
|
|
|
|
import ohos.agp.window.dialog.CommonDialog;
|
|
import ohos.agp.window.dialog.CommonDialog;
|
|
|
import ohos.app.Context;
|
|
import ohos.app.Context;
|
|
|
import ohos.eventhandler.EventHandler;
|
|
import ohos.eventhandler.EventHandler;
|
|
|
import ohos.eventhandler.EventRunner;
|
|
import ohos.eventhandler.EventRunner;
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Loading对话框类,继承CommonDialog
|
|
|
|
|
+ * 全屏遮罩,中间显示loading,不可点击,不可取消
|
|
|
|
|
+ */
|
|
|
|
|
+class LoadingCommonDialog extends CommonDialog {
|
|
|
|
|
+ private Component rootLayout;
|
|
|
|
|
+
|
|
|
|
|
+ public LoadingCommonDialog(Context context) {
|
|
|
|
|
+ super(context);
|
|
|
|
|
+ setTransparentBackground();
|
|
|
|
|
+ initDialog(context);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void initDialog(Context context) {
|
|
|
|
|
+ // 使用布局文件加载
|
|
|
|
|
+ rootLayout = LayoutScatter.getInstance(context)
|
|
|
|
|
+ .parse(ResourceTable.Layout_layout_loading_dialog, null, false);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取布局中的组件并设置属性
|
|
|
|
|
+ ProgressBar progressBar = (ProgressBar) rootLayout.findComponentById(ResourceTable.Id_progress_bar);
|
|
|
|
|
+ if (progressBar != null) {
|
|
|
|
|
+ progressBar.setProgressColor(Color.BLUE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Text text = (Text) rootLayout.findComponentById(ResourceTable.Id_text_loading);
|
|
|
|
|
+ if (text != null) {
|
|
|
|
|
+ // 文本已在布局文件中设置,这里可以保持默认或根据需要修改
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 设置对话框属性
|
|
|
|
|
+ setTransparent(true); // 设置对话框背景透明
|
|
|
|
|
+ setContentCustomComponent(rootLayout);
|
|
|
|
|
+ setAutoClosable(false); // 不可自动关闭
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void show() {
|
|
|
|
|
+ super.show();
|
|
|
|
|
+ // 在显示后设置窗口和根布局的透明背景
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 设置窗口和根布局的透明背景
|
|
|
|
|
+ */
|
|
|
|
|
+ private void setTransparentBackground() {
|
|
|
|
|
+ // 设置窗口背景为透明
|
|
|
|
|
+ RgbColor transparentColor = new RgbColor(Color.TRANSPARENT.getValue());
|
|
|
|
|
+ getWindow().setBackgroundColor(transparentColor);
|
|
|
|
|
+ getWindow().setLayoutInDisplaySideMode(1);
|
|
|
|
|
+ getWindow().setWindowLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Loading对话框工具类
|
|
* Loading对话框工具类
|
|
|
* 全屏遮罩,中间显示loading,不可点击,不可取消
|
|
* 全屏遮罩,中间显示loading,不可点击,不可取消
|
|
|
*/
|
|
*/
|
|
|
public class LoadingDialog {
|
|
public class LoadingDialog {
|
|
|
- private static CommonDialog currentDialog = null;
|
|
|
|
|
|
|
+ private static LoadingCommonDialog currentDialog = null;
|
|
|
private static int showCount = 0; // 显示计数,用于处理多个请求同时显示loading的情况
|
|
private static int showCount = 0; // 显示计数,用于处理多个请求同时显示loading的情况
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -28,7 +83,7 @@ public class LoadingDialog {
|
|
|
if (context == null) {
|
|
if (context == null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 获取主线程的 EventRunner
|
|
// 获取主线程的 EventRunner
|
|
|
EventRunner mainRunner = EventRunner.getMainEventRunner();
|
|
EventRunner mainRunner = EventRunner.getMainEventRunner();
|
|
|
if (mainRunner == null) {
|
|
if (mainRunner == null) {
|
|
@@ -36,7 +91,7 @@ public class LoadingDialog {
|
|
|
showDialog(context);
|
|
showDialog(context);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 创建主线程的 EventHandler
|
|
// 创建主线程的 EventHandler
|
|
|
EventHandler mainHandler = new EventHandler(mainRunner);
|
|
EventHandler mainHandler = new EventHandler(mainRunner);
|
|
|
mainHandler.postTask(() -> {
|
|
mainHandler.postTask(() -> {
|
|
@@ -55,7 +110,7 @@ public class LoadingDialog {
|
|
|
hideDialog();
|
|
hideDialog();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 创建主线程的 EventHandler
|
|
// 创建主线程的 EventHandler
|
|
|
EventHandler mainHandler = new EventHandler(mainRunner);
|
|
EventHandler mainHandler = new EventHandler(mainRunner);
|
|
|
mainHandler.postTask(() -> {
|
|
mainHandler.postTask(() -> {
|
|
@@ -74,76 +129,14 @@ public class LoadingDialog {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- // 创建固定大小的布局(不再全屏)
|
|
|
|
|
- DirectionalLayout rootLayout = new DirectionalLayout(context);
|
|
|
|
|
- // 设置固定大小,根据内容自适应
|
|
|
|
|
- DirectionalLayout.LayoutConfig rootConfig = new DirectionalLayout.LayoutConfig(
|
|
|
|
|
- DirectionalLayout.LayoutConfig.MATCH_CONTENT,
|
|
|
|
|
- DirectionalLayout.LayoutConfig.MATCH_CONTENT
|
|
|
|
|
- );
|
|
|
|
|
- rootLayout.setLayoutConfig(rootConfig);
|
|
|
|
|
- rootLayout.setAlignment(LayoutAlignment.CENTER);
|
|
|
|
|
- // 不设置背景,保持全透明
|
|
|
|
|
-
|
|
|
|
|
- // 创建内容容器(白色圆角卡片)
|
|
|
|
|
- DirectionalLayout contentLayout = new DirectionalLayout(context);
|
|
|
|
|
- DirectionalLayout.LayoutConfig contentConfig = new DirectionalLayout.LayoutConfig(
|
|
|
|
|
- DirectionalLayout.LayoutConfig.MATCH_CONTENT,
|
|
|
|
|
- DirectionalLayout.LayoutConfig.MATCH_CONTENT
|
|
|
|
|
- );
|
|
|
|
|
- contentLayout.setLayoutConfig(contentConfig);
|
|
|
|
|
- contentLayout.setOrientation(DirectionalLayout.VERTICAL);
|
|
|
|
|
- contentLayout.setAlignment(LayoutAlignment.CENTER);
|
|
|
|
|
- contentLayout.setPadding(40, 40, 40, 40);
|
|
|
|
|
-
|
|
|
|
|
- // 设置白色圆角背景
|
|
|
|
|
- ShapeElement contentBackground = new ShapeElement();
|
|
|
|
|
- contentBackground.setShape(ShapeElement.RECTANGLE);
|
|
|
|
|
- contentBackground.setRgbColor(new RgbColor(255, 255, 255, 255));
|
|
|
|
|
- contentBackground.setCornerRadius(16);
|
|
|
|
|
- contentLayout.setBackground(contentBackground);
|
|
|
|
|
-
|
|
|
|
|
- // 创建ProgressBar
|
|
|
|
|
- ProgressBar progressBar = new ProgressBar(context);
|
|
|
|
|
- DirectionalLayout.LayoutConfig progressConfig = new DirectionalLayout.LayoutConfig(
|
|
|
|
|
- 80, 80
|
|
|
|
|
- );
|
|
|
|
|
- progressBar.setLayoutConfig(progressConfig);
|
|
|
|
|
- progressBar.setProgressColor(Color.BLUE);
|
|
|
|
|
- progressBar.setIndeterminate(true); // 设置为无限循环
|
|
|
|
|
-
|
|
|
|
|
- // 创建提示文字(可选)
|
|
|
|
|
- Text text = new Text(context);
|
|
|
|
|
- DirectionalLayout.LayoutConfig textConfig = new DirectionalLayout.LayoutConfig(
|
|
|
|
|
- DirectionalLayout.LayoutConfig.MATCH_CONTENT,
|
|
|
|
|
- DirectionalLayout.LayoutConfig.MATCH_CONTENT
|
|
|
|
|
- );
|
|
|
|
|
- text.setLayoutConfig(textConfig);
|
|
|
|
|
- text.setText("加载中...");
|
|
|
|
|
- text.setTextSize(14);
|
|
|
|
|
- text.setTextColor(new Color(Color.getIntColor("#FF666666")));
|
|
|
|
|
- text.setMarginTop(16);
|
|
|
|
|
-
|
|
|
|
|
- // 添加到内容容器
|
|
|
|
|
- contentLayout.addComponent(progressBar);
|
|
|
|
|
- contentLayout.addComponent(text);
|
|
|
|
|
-
|
|
|
|
|
- // 添加到根布局
|
|
|
|
|
- rootLayout.addComponent(contentLayout);
|
|
|
|
|
- // 创建对话框
|
|
|
|
|
- CommonDialog dialog = new CommonDialog(context);
|
|
|
|
|
- // 注意:CommonDialog 在 API 7 中可能无法完全移除外层全屏蒙层
|
|
|
|
|
- // 只能通过 setTransparent(true) 设置对话框背景透明
|
|
|
|
|
- dialog.setTransparent(true); // 设置对话框背景透明
|
|
|
|
|
- dialog.setContentCustomComponent(rootLayout);
|
|
|
|
|
- dialog.setAutoClosable(false); // 不可自动关闭
|
|
|
|
|
-// dialog.setCanceledOnTouchOutside(false); // 点击外部不可关闭
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 创建自定义的Loading对话框
|
|
|
|
|
+ LoadingCommonDialog dialog = new LoadingCommonDialog(context);
|
|
|
|
|
+
|
|
|
// 显示对话框
|
|
// 显示对话框
|
|
|
dialog.show();
|
|
dialog.show();
|
|
|
currentDialog = dialog;
|
|
currentDialog = dialog;
|
|
|
showCount = 1;
|
|
showCount = 1;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
Log.error("显示Loading失败: " + e.getMessage());
|
|
Log.error("显示Loading失败: " + e.getMessage());
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
@@ -161,7 +154,7 @@ public class LoadingDialog {
|
|
|
try {
|
|
try {
|
|
|
// 减少计数
|
|
// 减少计数
|
|
|
showCount--;
|
|
showCount--;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 只有当计数为0时才真正关闭对话框
|
|
// 只有当计数为0时才真正关闭对话框
|
|
|
if (showCount <= 0) {
|
|
if (showCount <= 0) {
|
|
|
if (currentDialog.isShowing()) {
|
|
if (currentDialog.isShowing()) {
|