Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 73a4972

Browse files
committed
[FEAT] Update PitchView, support pitch change animation.
1 parent d8e231c commit 73a4972

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

lrcview/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 16
1010
targetSdkVersion 31
11-
versionCode 2
12-
versionName "1.0.1"
11+
versionCode 3
12+
versionName "1.0.2"
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles "consumer-rules.pro"
@@ -42,7 +42,7 @@ afterEvaluate {
4242
from components.release
4343
groupId = 'com.github.AgoraIO-Community'
4444
artifactId = 'LrcView-Android'
45-
version = '1.0.1'
45+
version = '1.0.2'
4646
}
4747
}
4848
}

lrcview/src/main/java/io/agora/lrcview/PitchView.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.agora.lrcview;
22

3+
import android.animation.ObjectAnimator;
4+
import android.animation.ValueAnimator;
35
import android.content.Context;
46
import android.content.res.TypedArray;
57
import android.graphics.Canvas;
@@ -8,7 +10,12 @@
810
import android.graphics.RectF;
911
import android.graphics.Shader;
1012
import android.os.Build;
13+
import android.os.Handler;
14+
import android.os.Looper;
1115
import android.util.AttributeSet;
16+
import android.util.FloatProperty;
17+
import android.util.Property;
18+
import android.util.TypedValue;
1219
import android.view.View;
1320

1421
import androidx.annotation.Nullable;
@@ -30,6 +37,7 @@ public class PitchView extends View {
3037
private static final int START_PERCENT = 40;
3138

3239
private static volatile LrcData lrcData;
40+
private Handler mHandler;
3341

3442
private float widthPerSecond = 0.2F;//1ms对应像素px
3543

@@ -38,6 +46,7 @@ public class PitchView extends View {
3846

3947
private int pitchMax = 0;//最大值
4048
private int pitchMin = 100;//最小值
49+
private int indicatorRadius;
4150

4251
private final Paint mPaint = new Paint();
4352
private int mNormalTextColor;
@@ -70,14 +79,20 @@ public PitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr
7079
}
7180

7281
private void init(@Nullable AttributeSet attrs) {
82+
indicatorRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 7, getResources().getDisplayMetrics());
7383
if (attrs == null) {
7484
return;
7585
}
76-
86+
this.mHandler = new Handler(Looper.myLooper());
7787
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.PitchView);
7888
mNormalTextColor = ta.getColor(R.styleable.PitchView_pitchNormalTextColor, getResources().getColor(R.color.lrc_normal_text_color));
7989
mDoneTextColor = ta.getColor(R.styleable.PitchView_pitchDoneTextColor, getResources().getColor(R.color.lrc_current_text_color));
8090
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+
8196
}
8297

8398
@Override
@@ -106,9 +121,32 @@ protected void onDraw(Canvas canvas) {
106121
super.onDraw(canvas);
107122

108123
drawStartLine(canvas);
124+
drawLocalPitch(canvas);
109125
drawItems(canvas);
110126
}
111127

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+
112150
private void drawStartLine(Canvas canvas) {
113151
mPaint.setShader(linearGradient);
114152
canvas.drawRect(0, 0, dotPointX, getHeight(), mPaint);
@@ -191,6 +229,28 @@ public void setLrcData(LrcData data) {
191229
}
192230

193231
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+
}
194254

195255
/**
196256
* 更新进度,单位毫秒

0 commit comments

Comments
 (0)