LrcProgress.java
package music.mic.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 处理歌词文件的类
*/
public class LrcProcess {
private List<LrcContent> LrcList;
private LrcContent mLrcContent;
public LrcProcess() {
mLrcContent = new LrcContent();
LrcList = new ArrayList<LrcContent>();
}
/**
* 读取歌词文件的内容
*/
public String readLRC(String song_path) {
// public void Read(String file){
StringBuilder stringBuilder = new StringBuilder();
File f = new File(song_path.replace(".mp3", ".lrc"));
try {
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, "GB2312");
BufferedReader br = new BufferedReader(isr);
String s = "";
while ((s = br.readLine()) != null) {
// if ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)
// || (s.indexOf("[by:") != -1)) {
// s = s.substring(s.indexOf(":") + 1, s.indexOf("]"));
// } else {
// try {
// String ss = s.substring(s.indexOf("["),
// s.indexOf("]") + 1);
// s = s.replace(ss, "");
// } catch (Exception e) {
// s = " ";
// }
// }
// stringBuilder.append(s + "\n");
// 替换字符
s = s.replace("[", "");
s = s.replace("]", "@");
// 分离"@"字符
String splitLrc_data[] = s.split("@");
if (splitLrc_data.length > 1) {
mLrcContent.setLrc(splitLrc_data[1]);
// 处理歌词取得歌曲时间
int LrcTime = TimeStr(splitLrc_data[0]);
mLrcContent.setLrc_time(LrcTime);
// 添加进列表数组
LrcList.add(mLrcContent);
// 创建对象
mLrcContent = new LrcContent();
}
}
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
stringBuilder.append("木有歌词文件,赶紧去下载!...");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
stringBuilder.append("木有读取到歌词啊!");
}
return stringBuilder.toString();
}
/**
* 解析歌曲时间处理类
*/
public int TimeStr(String timeStr) {
timeStr = timeStr.replace(":", ".");
timeStr = timeStr.replace(".", "@");
String timeData[] = timeStr.split("@");
// 分离出分、秒并转换为整型
int minute = Integer.parseInt(timeData[0]);
int second = Integer.parseInt(timeData[1]);
int millisecond = Integer.parseInt(timeData[2]);
// 计算上一行与下一行的时间转换为毫秒数
int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
return currentTime;
}
public List<LrcContent> getLrcContent() {
return LrcList;
}
/**
* 获得歌词和时间并返回的类
*/
public class LrcContent {
private String Lrc;
private int Lrc_time;
public String getLrc() {
return Lrc;
}
public void setLrc(String lrc) {
Lrc = lrc;
}
public int getLrc_time() {
return Lrc_time;
}
public void setLrc_time(int lrc_time) {
Lrc_time = lrc_time;
}
}
}
LrcView.java
package music.mic.util;
import java.util.ArrayList;
import java.util.List;
import music.mic.util.LrcProcess.LrcContent;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* 自定义绘画歌词,产生滚动效果
*/
public class LrcView extends TextView {
private float width;
private float high;
private Paint CurrentPaint;
private Paint NotCurrentPaint;
private float TextHigh = 25;
private float TextSize = 18;
private int Index = 0;
private List<LrcContent> mSentenceEntities = new ArrayList<LrcContent>();
public void setSentenceEntities(List<LrcContent> mSentenceEntities) {
this.mSentenceEntities = mSentenceEntities;
}
public LrcView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public LrcView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
public LrcView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
private void init() {
// TODO Auto-generated method stub
setFocusable(true);
// 高亮部分
CurrentPaint = new Paint();
CurrentPaint.setAntiAlias(true);
CurrentPaint.setTextAlign(Paint.Align.CENTER);
// 非高亮部分
NotCurrentPaint = new Paint();
NotCurrentPaint.setAntiAlias(true);
NotCurrentPaint.setTextAlign(Paint.Align.CENTER);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (canvas == null) {
return;
}
CurrentPaint.setColor(Color.argb(210, 251, 248, 29));
NotCurrentPaint.setColor(Color.argb(140, 255, 255, 255));
CurrentPaint.setTextSize(24);
CurrentPaint.setTypeface(Typeface.SERIF);
NotCurrentPaint.setTextSize(TextSize);
NotCurrentPaint.setTypeface(Typeface.DEFAULT);
try {
setText("");
canvas.drawText(mSentenceEntities.get(Index).getLrc(), width / 2,
high / 2, CurrentPaint);
float tempY = high / 2;
// 画出本句之前的句子
for (int i = Index - 1; i >= 0; i--) {
// 向上推移
tempY = tempY - TextHigh;
canvas.drawText(mSentenceEntities.get(i).getLrc(), width / 2,
tempY, NotCurrentPaint);
}
tempY = high / 2;
// 画出本句之后的句子
for (int i = Index + 1; i < mSentenceEntities.size(); i++) {
// 往下推移
tempY = tempY + TextHigh;
canvas.drawText(mSentenceEntities.get(i).getLrc(), width / 2,
tempY, NotCurrentPaint);
}
} catch (Exception e) {
setText("...木有歌词文件,赶紧去下载...");
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
this.width = w;
this.high = h;
}
public void SetIndex(int index) {
this.Index = index;
}
}
这里还涉及到一个anim文件
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:startOffset="700"
android:duration="1000">
</alpha>