Browse Source

增加云教练加载进度条

Pq 2 năm trước cách đây
mục cha
commit
16d0b8cb39

+ 12 - 0
BaseLibrary/src/main/res/drawable/progress_bar_ffffff_4dp.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- solid指定形状的填充色,只有android:color一个属性 -->
+    <solid android:color="@color/white" />
+    <!-- padding设置内容区域离边界的间距 -->
+    <!-- corners设置圆角,只适用于rectangle -->
+    <corners
+        android:bottomLeftRadius="4dp"
+        android:topLeftRadius="4dp"
+        android:bottomRightRadius="4dp"
+        android:topRightRadius="4dp"/>
+</shape>

+ 76 - 12
student/src/main/java/com/cooleshow/student/ui/web/AccompanyFragment.java

@@ -17,6 +17,7 @@ import android.media.SoundPool;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.Message;
 import android.os.SystemClock;
 import android.text.TextUtils;
@@ -131,6 +132,10 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
     private static final int SDK_PAY_FLAG = 1;
     private static final int SDK_AUTH_FLAG = 2;
     public static final int SHARE_CHAT_REQUEST_CODE = 501;
+    public static final int MESSAGE_TYPE_UPDATE_LOADING_START = 5010;//开始更新加载loading
+    public static final int MESSAGE_TYPE_UPDATE_LOADING = 5011;//更新加载loading
+    public static final int MESSAGE_TYPE_UPDATE_LOADING_COMPLETE = 5012;//完成加载loading
+    public static final int MESSAGE_TYPE_HIDE_LOADING = 5013;//隐藏加载loading
     FrameLayout viewParent;
     WebView webView;
     private UMShareListener mShareListener;
@@ -141,6 +146,41 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
     private URI webSocketUri = URI.create("BuildConfig.BASE_STU_SOCKET_URL");
     private String mImageBase64;
     private MusicTunerHelper mMusicTunerHelper;
+    private int currentProgressCount = 0;
+    private Handler mLoadHandler = new Handler(Looper.getMainLooper()) {
+        @Override
+        public void handleMessage(@androidx.annotation.NonNull Message msg) {
+            if (msg.what == MESSAGE_TYPE_UPDATE_LOADING) {
+                currentProgressCount++;
+                if (currentProgressCount <= 10) {
+                    updateProgress(4);
+                } else if (currentProgressCount <= 30) {
+                    updateProgress(2);
+                } else {
+                    updateProgress(1);
+                }
+                sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING, 1000);
+                return;
+            }
+
+            if (msg.what == MESSAGE_TYPE_UPDATE_LOADING_START) {
+                showLoadingAnim();
+                sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING, 1000);
+                return;
+            }
+
+            if (msg.what == MESSAGE_TYPE_UPDATE_LOADING_COMPLETE) {
+                mViewBinding.progress.setProgress(100);
+                sendProgressMessage(MESSAGE_TYPE_HIDE_LOADING, 300);
+                return;
+            }
+
+            if (msg.what == MESSAGE_TYPE_HIDE_LOADING) {
+                hideLoadingAnim();
+                return;
+            }
+        }
+    };
 
     public static AccompanyFragment newInstance(String url) {
         AccompanyFragment fragment = new AccompanyFragment();
@@ -265,6 +305,15 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
 
     }
 
+    private void updateProgress(int progress) {
+        int currentProgress = mViewBinding.progress.getProgress();
+        int totalProgress = currentProgress + progress;
+        if (totalProgress > 99) {
+            totalProgress = 99;
+        }
+        mViewBinding.progress.setProgress(totalProgress);
+    }
+
     @Override
     public void onSendMessage(String message) {
         Log.i("accom", "message:" + message);
@@ -377,32 +426,38 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
         try {
             JSONObject content = jsonObject.getJSONObject("content");
             boolean show = content.optBoolean("show", false);
-            if (getActivity() != null) {
-                getActivity().runOnUiThread(new Runnable() {
-                    @Override
-                    public void run() {
-                        if (show) {
-                            showLoadingAnim();
-                        } else {
-                            hideLoadingAnim();
-                        }
-                    }
-                });
+
+            if (mLoadHandler != null) {
+                mLoadHandler.removeCallbacksAndMessages(null);
+                if (show) {
+                    sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING_START, 0);
+                } else {
+                    sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING_COMPLETE, 0);
+                }
             }
             onSendMessage(jsonObject.toString());
         } catch (Exception e) {
-            hideLoadingAnim();
+            sendProgressMessage(MESSAGE_TYPE_HIDE_LOADING,0);
             e.printStackTrace();
         }
     }
 
     private void showLoadingAnim() {
+        currentProgressCount = 0;
         mViewBinding.ivLoadingBack.setVisibility(View.VISIBLE);
         mViewBinding.llLoading.setVisibility(View.VISIBLE);
         mViewBinding.viewLoadingAnim.playAnimation();
     }
 
+
+    private void sendProgressMessage(int type, int delayedTime) {
+        Message message = Message.obtain();
+        message.what = type;
+        mLoadHandler.sendMessageDelayed(message, delayedTime);
+    }
+
     private void hideLoadingAnim() {
+        currentProgressCount = 0;
         mViewBinding.ivLoadingBack.setVisibility(View.GONE);
         mViewBinding.viewLoadingAnim.clearAnimation();
         mViewBinding.llLoading.setVisibility(View.GONE);
@@ -1312,6 +1367,15 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
 
     @Override
     public void onDestroy() {
+        if (mHandler != null) {
+            mHandler.removeCallbacksAndMessages(null);
+        }
+        if (loopHandler != null) {
+            loopHandler.removeCallbacksAndMessages(null);
+        }
+        if (mLoadHandler != null) {
+            mLoadHandler.removeCallbacksAndMessages(null);
+        }
         super.onDestroy();
         if (null != webView) {
             webView.destroy();

+ 24 - 0
student/src/main/res/drawable/shape_accompany_loading_progress_drawable.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:id="@android:id/background">
+        <shape>
+            <corners android:radius="4dip" />
+<!--            <solid android:color="@color/color_eeeeee"/>-->
+            <stroke android:color="@color/white" android:width="1dp"/>
+        </shape>
+    </item>
+    <item android:id="@android:id/progress">
+<!--        <clip>-->
+<!--            <shape>-->
+<!--                <corners android:radius="4dip" />-->
+<!--                <solid android:color="@color/color_4877ff"/>-->
+<!--            </shape>-->
+<!--        </clip>-->
+
+        <!--
+   -->
+        <scale android:scaleWidth="100%"
+            android:drawable="@drawable/progress_bar_ffffff_4dp"/>
+    </item>
+</layer-list>

+ 15 - 1
student/src/main/res/layout/fragment_accompany.xml

@@ -3,6 +3,7 @@
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical">
 
     <FrameLayout
@@ -17,6 +18,7 @@
         android:layout_height="match_parent"
         android:background="@drawable/bg_accompany_loading"
         android:gravity="center"
+        tools:visibility="gone"
         android:orientation="vertical"
         android:visibility="gone">
 
@@ -37,9 +39,21 @@
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
-            android:text="加载中..."
+            android:text="小酷正在努力加载,请稍等..."
             android:textColor="@color/white"
             android:textSize="@dimen/sp_18" />
+
+        <ProgressBar
+            android:id="@+id/progress"
+            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
+            android:layout_width="350dp"
+            android:layout_height="8dp"
+            android:layout_marginStart="34dp"
+            android:layout_marginTop="16dp"
+            android:layout_marginEnd="34dp"
+            android:max="100"
+            android:progress="0"
+            android:progressDrawable="@drawable/shape_accompany_loading_progress_drawable" />
     </LinearLayout>
 
     <ImageView

+ 74 - 12
teacher/src/main/java/com/cooleshow/teacher/ui/web/AccompanyFragment.java

@@ -17,6 +17,7 @@ import android.media.SoundPool;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.Message;
 import android.os.SystemClock;
 import android.text.TextUtils;
@@ -133,6 +134,10 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
     private IWXAPI api;
     private static final int SDK_PAY_FLAG = 1;
     private static final int SDK_AUTH_FLAG = 2;
+    public static final int MESSAGE_TYPE_UPDATE_LOADING_START = 5010;//开始更新加载loading
+    public static final int MESSAGE_TYPE_UPDATE_LOADING = 5011;//更新加载loading
+    public static final int MESSAGE_TYPE_UPDATE_LOADING_COMPLETE = 5012;//完成加载loading
+    public static final int MESSAGE_TYPE_HIDE_LOADING = 5013;//隐藏加载loading
 
     FrameLayout viewParent;
     WebView webView;
@@ -144,6 +149,41 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
     private URI webSocketUri = URI.create("BuildConfig.BASE_STU_SOCKET_URL");
     private String mImageBase64;
     private MusicTunerHelper mMusicTunerHelper;
+    private int currentProgressCount = 0;
+    private Handler mLoadHandler = new Handler(Looper.getMainLooper()) {
+        @Override
+        public void handleMessage(@androidx.annotation.NonNull Message msg) {
+            if (msg.what == MESSAGE_TYPE_UPDATE_LOADING) {
+                currentProgressCount++;
+                if (currentProgressCount <= 10) {
+                    updateProgress(4);
+                } else if (currentProgressCount <= 30) {
+                    updateProgress(2);
+                } else {
+                    updateProgress(1);
+                }
+                sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING, 1000);
+                return;
+            }
+
+            if (msg.what == MESSAGE_TYPE_UPDATE_LOADING_START) {
+                showLoadingAnim();
+                sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING, 1000);
+                return;
+            }
+
+            if (msg.what == MESSAGE_TYPE_UPDATE_LOADING_COMPLETE) {
+                mViewBinding.progress.setProgress(100);
+                sendProgressMessage(MESSAGE_TYPE_HIDE_LOADING, 300);
+                return;
+            }
+
+            if (msg.what == MESSAGE_TYPE_HIDE_LOADING) {
+                hideLoadingAnim();
+                return;
+            }
+        }
+    };
 
     public static AccompanyFragment newInstance(String url) {
         AccompanyFragment fragment = new AccompanyFragment();
@@ -270,6 +310,21 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
 
     }
 
+    private void updateProgress(int progress) {
+        int currentProgress = mViewBinding.progress.getProgress();
+        int totalProgress = currentProgress + progress;
+        if (totalProgress > 99) {
+            totalProgress = 99;
+        }
+        mViewBinding.progress.setProgress(totalProgress);
+    }
+
+    private void sendProgressMessage(int type, int delayedTime) {
+        Message message = Message.obtain();
+        message.what = type;
+        mLoadHandler.sendMessageDelayed(message, delayedTime);
+    }
+
     @Override
     public void onSendMessage(String message) {
         Log.i("acc","message:"+message);
@@ -382,21 +437,18 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
         try {
             JSONObject content = jsonObject.getJSONObject("content");
             boolean show = content.optBoolean("show", false);
-            if (getActivity() != null) {
-                getActivity().runOnUiThread(new Runnable() {
-                    @Override
-                    public void run() {
-                        if (show) {
-                            showLoadingAnim();
-                        } else {
-                            hideLoadingAnim();
-                        }
-                    }
-                });
+
+            if (mLoadHandler != null) {
+                mLoadHandler.removeCallbacksAndMessages(null);
+                if (show) {
+                    sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING_START, 0);
+                } else {
+                    sendProgressMessage(MESSAGE_TYPE_UPDATE_LOADING_COMPLETE, 0);
+                }
             }
             onSendMessage(jsonObject.toString());
         } catch (Exception e) {
-            hideLoadingAnim();
+            sendProgressMessage(MESSAGE_TYPE_HIDE_LOADING,0);
             e.printStackTrace();
         }
     }
@@ -1317,6 +1369,16 @@ public class AccompanyFragment extends BaseMVPFragment<FragmentAccompanyBinding,
     @Override
     public void onDestroy() {
         super.onDestroy();
+        if (mHandler != null) {
+            mHandler.removeCallbacksAndMessages(null);
+        }
+        if (loopHandler != null) {
+            loopHandler.removeCallbacksAndMessages(null);
+        }
+        if (mLoadHandler != null) {
+            mLoadHandler.removeCallbacksAndMessages(null);
+        }
+
         if (null != webView) {
             webView.destroy();
         }

+ 24 - 0
teacher/src/main/res/drawable/shape_accompany_loading_progress_drawable.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:id="@android:id/background">
+        <shape>
+            <corners android:radius="4dip" />
+            <!--            <solid android:color="@color/color_eeeeee"/>-->
+            <stroke android:color="@color/white" android:width="1dp"/>
+        </shape>
+    </item>
+    <item android:id="@android:id/progress">
+        <!--        <clip>-->
+        <!--            <shape>-->
+        <!--                <corners android:radius="4dip" />-->
+        <!--                <solid android:color="@color/color_4877ff"/>-->
+        <!--            </shape>-->
+        <!--        </clip>-->
+
+        <!--
+   -->
+        <scale android:scaleWidth="100%"
+            android:drawable="@drawable/progress_bar_ffffff_4dp"/>
+    </item>
+</layer-list>

+ 13 - 1
teacher/src/main/res/layout/fragment_accompany.xml

@@ -37,9 +37,21 @@
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginTop="10dp"
-            android:text="加载中..."
+            android:text="小酷正在努力加载,请稍等..."
             android:textColor="@color/white"
             android:textSize="@dimen/sp_18" />
+
+        <ProgressBar
+            android:id="@+id/progress"
+            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
+            android:layout_width="350dp"
+            android:layout_height="8dp"
+            android:layout_marginStart="34dp"
+            android:layout_marginTop="16dp"
+            android:layout_marginEnd="34dp"
+            android:max="100"
+            android:progress="0"
+            android:progressDrawable="@drawable/shape_accompany_loading_progress_drawable"/>
     </LinearLayout>
 
     <ImageView