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

Commit

Permalink
[FEAT] Update PitchView, support pitch change animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
oOJohn6Oo committed Feb 9, 2022
1 parent d8e231c commit 73a4972
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lrcview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 31
versionCode 2
versionName "1.0.1"
versionCode 3
versionName "1.0.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down Expand Up @@ -42,7 +42,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.AgoraIO-Community'
artifactId = 'LrcView-Android'
version = '1.0.1'
version = '1.0.2'
}
}
}
Expand Down
62 changes: 61 additions & 1 deletion lrcview/src/main/java/io/agora/lrcview/PitchView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.agora.lrcview;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
Expand All @@ -8,7 +10,12 @@
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.FloatProperty;
import android.util.Property;
import android.util.TypedValue;
import android.view.View;

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

private static volatile LrcData lrcData;
private Handler mHandler;

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

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

private int pitchMax = 0;//最大值
private int pitchMin = 100;//最小值
private int indicatorRadius;

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

private void init(@Nullable AttributeSet attrs) {
indicatorRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 7, getResources().getDisplayMetrics());
if (attrs == null) {
return;
}

this.mHandler = new Handler(Looper.myLooper());
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.PitchView);
mNormalTextColor = ta.getColor(R.styleable.PitchView_pitchNormalTextColor, getResources().getColor(R.color.lrc_normal_text_color));
mDoneTextColor = ta.getColor(R.styleable.PitchView_pitchDoneTextColor, getResources().getColor(R.color.lrc_current_text_color));
ta.recycle();

int startColor = getResources().getColor(R.color.pitch_start);
int endColor = getResources().getColor(R.color.pitch_end);
linearGradient = new LinearGradient(dotPointX, 0, 0, 0, startColor, endColor, Shader.TileMode.CLAMP);

}

@Override
Expand Down Expand Up @@ -106,9 +121,32 @@ protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

drawStartLine(canvas);
drawLocalPitch(canvas);
drawItems(canvas);
}

private void drawLocalPitch(Canvas canvas) {
mPaint.setShader(null);
mPaint.setColor(mNormalTextColor);
float value = getPitchHeight();
if(value >= 0){
canvas.drawCircle(dotPointX, value, indicatorRadius, mPaint);
}
}

private float getPitchHeight() {
float res = 0;
if(mLocalPitch!=0 && pitchMax != 0 && pitchMin != 100){
float realPitchMax = pitchMax + 5;
float realPitchMin = pitchMin - 5;
res = (float) (1 - ((mLocalPitch - pitchMin) / (realPitchMax - realPitchMin)) ) * getHeight();
}
else if(mLocalPitch == 0){
res = getHeight();
}
return res;
}

private void drawStartLine(Canvas canvas) {
mPaint.setShader(linearGradient);
canvas.drawRect(0, 0, dotPointX, getHeight(), mPaint);
Expand Down Expand Up @@ -191,6 +229,28 @@ public void setLrcData(LrcData data) {
}

private long mCurrentTime = 0;
private float mLocalPitch = 0;

private void setMLocalPitch(float mLocalPitch) {
this.mLocalPitch = mLocalPitch;
}
/**
* 更新音调
*
* @param pitch 单位hz
*/
public void updateLocalPitch(double pitch) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if(mLocalPitch == pitch){
mLocalPitch = 0;
}
}
}, 2000L);
ObjectAnimator.ofFloat(this, "mLocalPitch", this.mLocalPitch, (float) pitch).setDuration(50).start();
invalidate();
}

/**
* 更新进度,单位毫秒
Expand Down

0 comments on commit 73a4972

Please sign in to comment.