浏览代码

0.0.50.beta9 ready
- 针对 PopMenu、GuideDialog 针对绑定的 baseView 位置异常情况的处理;
- DialogXValueAnimator 修改为支持屏幕最高刷新率;

Kongzue 1 年之前
父节点
当前提交
258396f761

+ 11 - 5
DialogX/src/main/java/com/kongzue/dialogx/dialogs/CustomDialog.java

@@ -264,8 +264,8 @@ public class CustomDialog extends BaseDialog {
                     Runnable onLayoutChangeRunnable = new Runnable() {
                         @Override
                         public void run() {
-                            int baseViewLeft = baseViewLoc[0] - (int)boxRoot.getX();
-                            int baseViewTop = baseViewLoc[1] - (int)boxRoot.getY();
+                            int baseViewLeft = baseViewLoc[0] - (int) boxRoot.getX();
+                            int baseViewTop = baseViewLoc[1] - (int) boxRoot.getY();
                             int calX = 0, calY = 0;
                             if (alignViewGravity != -1) {
                                 if (isAlignBaseViewGravity(Gravity.CENTER_VERTICAL)) {
@@ -311,9 +311,13 @@ public class CustomDialog extends BaseDialog {
                             int[] baseViewLocCache = new int[2];
                             if (baseView() != null) {
                                 baseView().getLocationInWindow(baseViewLocCache);
-                                if (getDialogImpl() != null && isShow) {
-                                    baseViewLoc[0] = baseViewLocCache[0];
-                                    baseViewLoc[1] = baseViewLocCache[1];
+                                if (getDialogImpl() != null && isShow && baseView().getVisibility() == View.VISIBLE) {
+                                    if (baseViewLocCache[0] != 0) {
+                                        baseViewLoc[0] = baseViewLocCache[0];
+                                    }
+                                    if (baseViewLocCache[1] != 0) {
+                                        baseViewLoc[1] = baseViewLocCache[1];
+                                    }
                                     onLayoutChangeRunnable.run();
                                 }
                             } else {
@@ -761,6 +765,7 @@ public class CustomDialog extends BaseDialog {
 
     /**
      * 改为使用 .setEnableImmersiveMode(boolean) 来控制是否适配沉浸式
+     *
      * @param autoUnsafePlacePadding 是否适配沉浸式
      * @return CustomDialog
      */
@@ -772,6 +777,7 @@ public class CustomDialog extends BaseDialog {
 
     /**
      * 改为使用 .setEnableImmersiveMode(boolean) 来控制是否适配沉浸式
+     *
      * @param fullscreen 是否适配沉浸式
      * @return CustomDialog
      */

+ 1 - 1
DialogX/src/main/java/com/kongzue/dialogx/dialogs/PopMenu.java

@@ -265,7 +265,7 @@ public class PopMenu extends BaseDialog {
                     int[] baseViewLocCache = new int[2];
                     if (baseView() != null) {
                         baseView().getLocationInWindow(baseViewLocCache);
-                        if (getDialogImpl() != null && !baseViewLoc.isSameLoc(baseViewLocCache)) {
+                        if (getDialogImpl() != null && !baseViewLoc.isSameLoc(baseViewLocCache) && baseView().getVisibility() == VISIBLE) {
                             baseViewLoc.set(baseViewLocCache);
                             refreshMenuLoc();
                         }

+ 11 - 6
DialogX/src/main/java/com/kongzue/dialogx/util/DialogXValueAnimator.java

@@ -21,15 +21,12 @@ public class DialogXValueAnimator {
     private float endValue;
     private int repeatCount = 0;
     private int currentRepeatCount = 0;
+    private int refreshInterval = 16;       // 控制更新频率,默认 60 FPS
 
     public static DialogXValueAnimator ofFloat(float start, float end){
         return new DialogXValueAnimator(start, end);
     }
 
-    public DialogXValueAnimator(long duration) {
-        this.duration = duration;
-    }
-
     public DialogXValueAnimator(float startValue, float endValue) {
         this.startValue = startValue;
         this.endValue = endValue;
@@ -94,8 +91,7 @@ public class DialogXValueAnimator {
                     }
 
                     try {
-                        // 控制更新频率,可以根据需要调整
-                        Thread.sleep(16); // 60 FPS
+                        Thread.sleep(refreshInterval);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
@@ -192,4 +188,13 @@ public class DialogXValueAnimator {
     public int getCurrentRepeatCount() {
         return currentRepeatCount;
     }
+
+    public int getRefreshInterval() {
+        return refreshInterval;
+    }
+
+    public DialogXValueAnimator setRefreshInterval(int refreshInterval) {
+        this.refreshInterval = refreshInterval;
+        return this;
+    }
 }

+ 26 - 5
DialogX/src/main/java/com/kongzue/dialogx/util/DialogXViewLoc.java

@@ -1,6 +1,9 @@
 package com.kongzue.dialogx.util;
 
 public class DialogXViewLoc {
+
+    public static boolean skipErrorLoc = true;
+
     private float x;
     private float y;
     private float w;
@@ -42,7 +45,7 @@ public class DialogXViewLoc {
         return this;
     }
 
-    public boolean isSameLoc(int[] loc){
+    public boolean isSameLoc(int[] loc) {
         if (loc.length == 2) {
             return x == loc[0] && y == loc[1];
         }
@@ -54,12 +57,30 @@ public class DialogXViewLoc {
 
     public void set(int[] loc) {
         if (loc.length == 2) {
-            x = loc[0];
-            y = loc[1];
+            if (skipErrorLoc) {
+                if (loc[0] != 0) {
+                    x = loc[0];
+                }
+                if (loc[1] != 0) {
+                    y = loc[1];
+                }
+            } else {
+                x = loc[0];
+                y = loc[1];
+            }
         }
         if (loc.length == 4) {
-            x = loc[0];
-            y = loc[1];
+            if (skipErrorLoc) {
+                if (loc[0] != 0) {
+                    x = loc[0];
+                }
+                if (loc[1] != 0) {
+                    y = loc[1];
+                }
+            } else {
+                x = loc[0];
+                y = loc[1];
+            }
             w = loc[2];
             h = loc[3];
         }

+ 35 - 0
DialogX/src/main/java/com/kongzue/dialogx/util/views/ProgressView.java

@@ -9,11 +9,15 @@ import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Path;
 import android.graphics.RectF;
+import android.os.Build;
 import android.os.Handler;
 import android.os.Looper;
 import android.util.AttributeSet;
+import android.util.Log;
+import android.view.Display;
 import android.view.HapticFeedbackConstants;
 import android.view.View;
+import android.view.WindowManager;
 import android.view.animation.AccelerateDecelerateInterpolator;
 import android.view.animation.AccelerateInterpolator;
 import android.view.animation.DecelerateInterpolator;
@@ -105,15 +109,21 @@ public class ProgressView extends View implements ProgressViewInterface {
             mPaint.setColor(color);
 
             if (!isInEditMode()) {
+                int refreshInterval = (int) calculateMillisPerFrame(getContext());
+
                 halfSweepA = (halfSweepAMaxValue - halfSweepAMinValue) / 2;
 
                 rotateAnimator = DialogXValueAnimator.ofFloat(0, 365);
                 rotateAnimator.setDuration(1000);
                 rotateAnimator.setInterpolator(new LinearInterpolator());
                 rotateAnimator.setRepeatCount(-1);
+                rotateAnimator.setRefreshInterval(refreshInterval);
                 rotateAnimator.addUpdateListener(new DialogXValueAnimator.ValueUpdateListener() {
                     @Override
                     public void onValueUpdate(float animatedValue) {
+                        if (!isAttachedToWindow()){
+                            return;
+                        }
                         currentRotateDegrees = animatedValue;
                         invalidate();
                     }
@@ -121,6 +131,7 @@ public class ProgressView extends View implements ProgressViewInterface {
 
                 followAnimator = DialogXValueAnimator.ofFloat(0, 365);
                 followAnimator.setDuration(1500);
+                followAnimator.setRefreshInterval(refreshInterval);
                 followAnimator.setInterpolator(new LinearInterpolator());
                 followAnimator.setRepeatCount(-1);
                 followAnimator.addUpdateListener(new DialogXValueAnimator.ValueUpdateListener() {
@@ -502,4 +513,28 @@ public class ProgressView extends View implements ProgressViewInterface {
         final float scale = Resources.getSystem().getDisplayMetrics().density;
         return (int) (dpValue * scale + 0.5f);
     }
+
+    private float getRefreshRate(Context context) {
+        float refreshRate = 60.0f; // 默认值
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
+            Display display = context.getSystemService(WindowManager.class).getDefaultDisplay();
+            Display.Mode mode = display.getMode();
+            refreshRate = mode.getRefreshRate();
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
+            refreshRate = display.getRefreshRate();
+        }
+
+        return refreshRate;
+    }
+
+    private long calculateMillisPerFrame(Context context) {
+        float refreshRate = getRefreshRate(context);
+
+        if (refreshRate > 0) {
+            return (long) (1000.0 / refreshRate);
+        } else {
+            return 16; // 获取刷新率失败
+        }
+    }
 }

+ 2 - 2
app/release/output-metadata.json

@@ -11,8 +11,8 @@
       "type": "SINGLE",
       "filters": [],
       "attributes": [],
-      "versionCode": 48,
-      "versionName": "0.0.49.beta6",
+      "versionCode": 49,
+      "versionName": "0.0.50.beta8",
       "outputFile": "app-release.apk"
     }
   ],

+ 0 - 17
app/src/main/java/com/kongzue/dialogxdemo/activity/MainActivity.java

@@ -448,23 +448,6 @@ public class MainActivity extends BaseActivity {
                                 }
                                 return false;
                             }
-                        })
-                        .setOnIconChangeCallBack(new OnIconChangeCallBack<PopMenu>(true) {
-                            @Override
-                            public int getIcon(PopMenu dialog, int index, String menuText) {
-                                switch (menuText) {
-                                    case "添加":
-                                        return R.mipmap.img_dialogx_demo_add;
-                                    case "编辑":
-                                        return R.mipmap.img_dialogx_demo_edit;
-                                    case "删除":
-                                        return R.mipmap.img_dialogx_demo_delete;
-                                    case "分享":
-                                        return R.mipmap.img_dialogx_demo_share;
-                                    default:
-                                        return 0;
-                                }
-                            }
                         });
             }
         });

+ 1 - 1
gradle.properties

@@ -19,7 +19,7 @@ android.useAndroidX=true
 # Automatically convert third-party libraries to use AndroidX
 android.enableJetifier=true
 
-BUILD_VERSION=0.0.50.beta8
+BUILD_VERSION=0.0.50.beta9
 BUILD_VERSION_INT=49
 DIALOGX_STYLE_VERSION=5
 android.nonTransitiveRClass=true