parent
417e10a763
commit
14c85c581f
@ -1,192 +0,0 @@
|
||||
package com.example.musicplayer.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.media.MediaPlayer;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import com.example.musicplayer.R;
|
||||
import com.example.musicplayer.entiy.LrcBean;
|
||||
import com.example.musicplayer.util.LrcUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* author : 残渊
|
||||
* time : 2019/03/18
|
||||
* desc : 自定义歌词界面
|
||||
* </pre>
|
||||
*/
|
||||
|
||||
public class LrcView extends View {
|
||||
|
||||
private List<LrcBean> list; //歌词列表
|
||||
private Paint gPaint; //静态歌词画笔
|
||||
private Paint hPaint; //动态歌词画笔
|
||||
private int width = 0, height = 0;
|
||||
private int currentPosition = 0; //歌曲播放的位置
|
||||
private MediaPlayer player;
|
||||
private int lastPosition = 0;
|
||||
private int highLineColor; //播放歌词的颜色
|
||||
private int lrcTextSize; //歌词大小
|
||||
private int lrcColor; //歌词颜色
|
||||
private int mode = 0;
|
||||
public final static int KARAOKE = 1;
|
||||
|
||||
public void setHighLineColor(int highLineColor) {
|
||||
this.highLineColor = highLineColor;
|
||||
}
|
||||
|
||||
public void setLrcColor(int lrcColor) {
|
||||
this.lrcColor = lrcColor;
|
||||
}
|
||||
|
||||
public void setMode(int mode) {
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
public void setPlayer(MediaPlayer player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param lrc 标准歌词字符串
|
||||
*/
|
||||
public void setLrc(String lrc) {
|
||||
list = LrcUtil.parseStr2List(lrc);
|
||||
}
|
||||
|
||||
public LrcView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public LrcView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public LrcView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
|
||||
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LrcView);
|
||||
highLineColor = ta.getColor(R.styleable.LrcView_hignLineColor, getResources().getColor(R.color.green));
|
||||
lrcColor = ta.getColor(R.styleable.LrcView_lrcColor, getResources().getColor(android.R.color.darker_gray));
|
||||
float densityText = getResources().getDisplayMetrics().scaledDensity;
|
||||
lrcTextSize = ta.getDimensionPixelSize(R.styleable.LrcView_lrcTextSize,(int)(16*densityText));
|
||||
mode = ta.getInt(R.styleable.LrcView_lrcMode,mode);
|
||||
ta.recycle();
|
||||
gPaint = new Paint();
|
||||
gPaint.setAntiAlias(true);
|
||||
gPaint.setColor(lrcColor);
|
||||
gPaint.setTextSize(lrcTextSize);
|
||||
gPaint.setTextAlign(Paint.Align.CENTER);
|
||||
hPaint = new Paint();
|
||||
hPaint.setAntiAlias(true);
|
||||
hPaint.setColor(highLineColor);
|
||||
hPaint.setTextSize(lrcTextSize);
|
||||
hPaint.setTextAlign(Paint.Align.CENTER);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
if (width == 0 || height == 0) {
|
||||
width = getMeasuredWidth();
|
||||
height = getMeasuredHeight();
|
||||
}
|
||||
if (list == null || list.size() == 0) {
|
||||
canvas.drawText("暂无歌词", width / 2, height / 2, gPaint);
|
||||
return;
|
||||
}
|
||||
|
||||
getCurrentPosition();
|
||||
int currentMillis = player.getCurrentPosition();
|
||||
drawLrc2(canvas, currentMillis);
|
||||
long start = list.get(currentPosition).getStart();
|
||||
float v = (currentMillis - start) > 500 ? currentPosition * 120 : lastPosition * 120 + (currentPosition - lastPosition) * 120 * ((currentMillis - start) / 500f);
|
||||
setScrollY((int) v);
|
||||
if (getScrollY() == currentPosition * 120) {
|
||||
lastPosition = currentPosition;
|
||||
}
|
||||
postInvalidateDelayed(100);
|
||||
}
|
||||
|
||||
private void drawLrc2(Canvas canvas, int currentMillis) {
|
||||
if (mode == 0) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (i == currentPosition) {
|
||||
canvas.drawText(list.get(i).getLrc(), width / 2, height / 2 + 120 * i, hPaint);
|
||||
} else {
|
||||
canvas.drawText(list.get(i).getLrc(), width / 2, height / 2 + 120 * i, gPaint);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
canvas.drawText(list.get(i).getLrc(), width / 2, height / 2 + 80 * i, gPaint);
|
||||
}
|
||||
String highLineLrc = list.get(currentPosition).getLrc();
|
||||
int highLineWidth = (int) gPaint.measureText(highLineLrc);
|
||||
int leftOffset = (width - highLineWidth) / 2;
|
||||
LrcBean lrcBean = list.get(currentPosition);
|
||||
long start = lrcBean.getStart();
|
||||
long end = lrcBean.getEnd();
|
||||
int i = (int) ((currentMillis - start) * 1.0f / (end - start) * highLineWidth);
|
||||
if (i > 0) {
|
||||
Bitmap textBitmap = Bitmap.createBitmap(i, 80, Bitmap.Config.ARGB_8888);
|
||||
Canvas textCanvas = new Canvas(textBitmap);
|
||||
textCanvas.drawText(highLineLrc, highLineWidth / 2, 80, hPaint);
|
||||
canvas.drawBitmap(textBitmap, leftOffset, height / 2 + 80 * (currentPosition - 1), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
currentPosition = 0;
|
||||
lastPosition = 0;
|
||||
setScrollY(0);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
private void drawLrc1(Canvas canvas) {
|
||||
String text = list.get(currentPosition).getLrc();
|
||||
canvas.drawText(text, width / 2, height / 2, hPaint);
|
||||
|
||||
for (int i = 1; i < 10; i++) {
|
||||
int index = currentPosition - i;
|
||||
if (index > -1) {
|
||||
canvas.drawText(list.get(index).getLrc(), width / 2, height / 2 - 100 * i, gPaint);
|
||||
}
|
||||
}
|
||||
for (int i = 1; i < 10; i++) {
|
||||
int index = currentPosition + i;
|
||||
if (index < list.size()) {
|
||||
canvas.drawText(list.get(index).getLrc(), width / 2, height / 2 + 100 * i, gPaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void getCurrentPosition() {
|
||||
try {
|
||||
int currentMillis = player.getCurrentPosition();
|
||||
if (currentMillis < list.get(0).getStart()) {
|
||||
currentPosition = 0;
|
||||
return;
|
||||
}
|
||||
if (currentMillis > list.get(list.size() - 1).getStart()) {
|
||||
currentPosition = list.size() - 1;
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (currentMillis >= list.get(i).getStart() && currentMillis < list.get(i).getEnd()) {
|
||||
currentPosition = i;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
postInvalidateDelayed(100);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue