Browse Source

修改调音器指针偏移效果

Pq 1 year ago
parent
commit
0c873e3971

+ 2 - 0
musictuner/src/main/java/com/cooleshow/musictuner/constants/MusicTunerConstants.java

@@ -18,4 +18,6 @@ public class MusicTunerConstants {
             "B♭", "C", "B♭"};
 
     public final static int CORRECT_VALUE_RANGE =10;//音频值差值正确值范围
+
+    public final static int TUNER_POINTER_ANIMATION =220;//调音器指针动画持续时间,建议不要修改了
 }

+ 57 - 2
musictuner/src/main/java/com/cooleshow/musictuner/widget/DashBoardView.java

@@ -1,5 +1,6 @@
 package com.cooleshow.musictuner.widget;
 
+import android.animation.ValueAnimator;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Canvas;
@@ -19,7 +20,9 @@ import android.util.Log;
 import android.util.TypedValue;
 import android.view.View;
 
+import com.cooleshow.base.utils.LOG;
 import com.cooleshow.base.utils.SizeUtils;
+import com.cooleshow.musictuner.constants.MusicTunerConstants;
 
 import androidx.annotation.Nullable;
 
@@ -52,12 +55,14 @@ public class DashBoardView extends View {
     private float pointerHeight = 0;//指针高度
     private double innerGradientRadiusPercent = 0.75;//渐变线的圆半径比例
     private int pointerAngle = 0;
-    private int currentProgress = 0;
+    private float currentProgress = 0;
     private RectF whiteAreaRectF;
     private Paint mWhiteAreaPaint;
     private int whiteAreaStartColor;
     private int whiteAreaEndColor;
 
+    private ValueAnimator mAnimator;
+
     public DashBoardView(Context context) {
         this(context, null);
     }
@@ -168,6 +173,15 @@ public class DashBoardView extends View {
 //        canvas.drawLine(cx, cy, cx, 0, mCenterPointPaint);
         canvas.drawArc(whiteAreaRectF, -108, 36, true, mWhiteAreaPaint);
         canvas.drawArc(mGradientLineRectF, -180, 180, false, mOuterGradientPaint);
+
+        LOG.i("onDraw:" + currentProgress);
+        currentProgress += offsetValue;
+        if (currentProgress >= maxMusicHzOffset) {
+            currentProgress = maxMusicHzOffset;
+        }
+        if (currentProgress < minMusicHzOffset) {
+            currentProgress = minMusicHzOffset;
+        }
         float[] floats = countPointerPosition(currentProgress);
         canvas.drawLine(cx, cy, floats[0], floats[1], mPointerPaint);
 
@@ -299,8 +313,49 @@ public class DashBoardView extends View {
 
     public void setProgress(int progress) {
         if (progress != -100) {
-            this.currentProgress = progress;
+//            this.currentProgress = progress;
+            LOG.i("setProgress:" + progress);
+            if (progress >= maxMusicHzOffset) {
+                progress = maxMusicHzOffset;
+            }
+            if (progress <= minMusicHzOffset) {
+                progress = minMusicHzOffset;
+            }
+            offsetValue = 0;
+            currentOffsetValue = 0;
+            startAnimation(progress - currentProgress);
             invalidate();
         }
     }
+
+    private float lastAnimProgress = 0f;
+    private float offsetValue = 0;
+    private float currentOffsetValue = 0;
+
+    private void startAnimation(float progress) {
+        LOG.i("startAnimation:" + progress);
+        this.currentOffsetValue = progress;
+        if (mAnimator == null) {
+            mAnimator = ValueAnimator.ofFloat(0f, 1f);
+
+            mAnimator.setDuration(MusicTunerConstants.TUNER_POINTER_ANIMATION); // 时长为200秒
+            mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+                @Override
+                public void onAnimationUpdate(ValueAnimator animation) {
+                    float value = (float) animation.getAnimatedValue();
+                    LOG.i("onAnimationUpdate:" + value + "--当前偏移总量:" + currentOffsetValue);
+                    float d = currentOffsetValue * value;
+                    LOG.i("d:" + d);
+                    offsetValue = (value-lastAnimProgress)*currentOffsetValue;
+                    lastAnimProgress =value;
+                    LOG.i("offsetValue:" + offsetValue);
+                    invalidate();
+                }
+            });
+        }
+        if (mAnimator.isRunning()) {
+            mAnimator.cancel();
+        }
+        mAnimator.start();
+    }
 }