1
1
package io .agora .lrcview ;
2
2
3
+ import android .animation .ObjectAnimator ;
4
+ import android .animation .ValueAnimator ;
3
5
import android .content .Context ;
4
6
import android .content .res .TypedArray ;
5
7
import android .graphics .Canvas ;
8
10
import android .graphics .RectF ;
9
11
import android .graphics .Shader ;
10
12
import android .os .Build ;
13
+ import android .os .Handler ;
14
+ import android .os .Looper ;
11
15
import android .util .AttributeSet ;
16
+ import android .util .FloatProperty ;
17
+ import android .util .Property ;
18
+ import android .util .TypedValue ;
12
19
import android .view .View ;
13
20
14
21
import androidx .annotation .Nullable ;
@@ -30,6 +37,7 @@ public class PitchView extends View {
30
37
private static final int START_PERCENT = 40 ;
31
38
32
39
private static volatile LrcData lrcData ;
40
+ private Handler mHandler ;
33
41
34
42
private float widthPerSecond = 0.2F ;//1ms对应像素px
35
43
@@ -38,6 +46,7 @@ public class PitchView extends View {
38
46
39
47
private int pitchMax = 0 ;//最大值
40
48
private int pitchMin = 100 ;//最小值
49
+ private int indicatorRadius ;
41
50
42
51
private final Paint mPaint = new Paint ();
43
52
private int mNormalTextColor ;
@@ -70,14 +79,20 @@ public PitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr
70
79
}
71
80
72
81
private void init (@ Nullable AttributeSet attrs ) {
82
+ indicatorRadius = (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , 7 , getResources ().getDisplayMetrics ());
73
83
if (attrs == null ) {
74
84
return ;
75
85
}
76
-
86
+ this . mHandler = new Handler ( Looper . myLooper ());
77
87
TypedArray ta = getContext ().obtainStyledAttributes (attrs , R .styleable .PitchView );
78
88
mNormalTextColor = ta .getColor (R .styleable .PitchView_pitchNormalTextColor , getResources ().getColor (R .color .lrc_normal_text_color ));
79
89
mDoneTextColor = ta .getColor (R .styleable .PitchView_pitchDoneTextColor , getResources ().getColor (R .color .lrc_current_text_color ));
80
90
ta .recycle ();
91
+
92
+ int startColor = getResources ().getColor (R .color .pitch_start );
93
+ int endColor = getResources ().getColor (R .color .pitch_end );
94
+ linearGradient = new LinearGradient (dotPointX , 0 , 0 , 0 , startColor , endColor , Shader .TileMode .CLAMP );
95
+
81
96
}
82
97
83
98
@ Override
@@ -106,9 +121,32 @@ protected void onDraw(Canvas canvas) {
106
121
super .onDraw (canvas );
107
122
108
123
drawStartLine (canvas );
124
+ drawLocalPitch (canvas );
109
125
drawItems (canvas );
110
126
}
111
127
128
+ private void drawLocalPitch (Canvas canvas ) {
129
+ mPaint .setShader (null );
130
+ mPaint .setColor (mNormalTextColor );
131
+ float value = getPitchHeight ();
132
+ if (value >= 0 ){
133
+ canvas .drawCircle (dotPointX , value , indicatorRadius , mPaint );
134
+ }
135
+ }
136
+
137
+ private float getPitchHeight () {
138
+ float res = 0 ;
139
+ if (mLocalPitch !=0 && pitchMax != 0 && pitchMin != 100 ){
140
+ float realPitchMax = pitchMax + 5 ;
141
+ float realPitchMin = pitchMin - 5 ;
142
+ res = (float ) (1 - ((mLocalPitch - pitchMin ) / (realPitchMax - realPitchMin )) ) * getHeight ();
143
+ }
144
+ else if (mLocalPitch == 0 ){
145
+ res = getHeight ();
146
+ }
147
+ return res ;
148
+ }
149
+
112
150
private void drawStartLine (Canvas canvas ) {
113
151
mPaint .setShader (linearGradient );
114
152
canvas .drawRect (0 , 0 , dotPointX , getHeight (), mPaint );
@@ -191,6 +229,28 @@ public void setLrcData(LrcData data) {
191
229
}
192
230
193
231
private long mCurrentTime = 0 ;
232
+ private float mLocalPitch = 0 ;
233
+
234
+ private void setMLocalPitch (float mLocalPitch ) {
235
+ this .mLocalPitch = mLocalPitch ;
236
+ }
237
+ /**
238
+ * 更新音调
239
+ *
240
+ * @param pitch 单位hz
241
+ */
242
+ public void updateLocalPitch (double pitch ) {
243
+ mHandler .postDelayed (new Runnable () {
244
+ @ Override
245
+ public void run () {
246
+ if (mLocalPitch == pitch ){
247
+ mLocalPitch = 0 ;
248
+ }
249
+ }
250
+ }, 2000L );
251
+ ObjectAnimator .ofFloat (this , "mLocalPitch" , this .mLocalPitch , (float ) pitch ).setDuration (50 ).start ();
252
+ invalidate ();
253
+ }
194
254
195
255
/**
196
256
* 更新进度,单位毫秒
0 commit comments