本文主要是介绍android歌词显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本类继承于TextView,参考了其它的代码,经过测试调试,这个是基础的模型,有兴趣的同事可以在此基础上改善界面,不多说了,贴上代码:
public class LrcView extends TextView {// paint for line lrc (normal : current highlight : other)private Paint normal, highlight;// line distanceprivate static final int DY = 30;// middle of screen heightprivate float middleY;// middle of screen widthprivate float mX;// height of screenprivate float mY;// current lryc lineprivate int line = 0;// when lryc display thread start,it means whether lrc start or pause rollboolean startorpause = false;// lrcy display threadprivate Thread LrcViewThread;// update ui handlerprivate Handler LrcViewHandler;// update ui runnableprivate Runnable mRunnable;// lrcy display data constractpublic class LrcViewData {public long MilliSecond;public String LrcLine;}// lrcy display data listprivate List<LrcViewData> ListViewData;// constract functionpublic LrcView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubLrcViewInitData();}// constract functionpublic LrcView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubLrcViewInitData();}// constract functionpublic LrcView(Context context) {super(context);// TODO Auto-generated constructor stubLrcViewInitData();}/*** dscript : init lryc display data contain pain(size, color, style) ,new a* data list, creat a safety method for update ui* * @author zhangdong* @param null* @return null* */private void LrcViewInitData() {ListViewData = new ArrayList<LrcViewData>();// normal paintnormal = new Paint();normal.setAntiAlias(true);normal.setTextSize(10);normal.setColor(getResources().getColor(R.color.white));normal.setTypeface(Typeface.SERIF);// highlighthighlight = new Paint();highlight.setAntiAlias(true);highlight.setColor(getResources().getColor(R.color.red));highlight.setTextSize(10);highlight.setTypeface(Typeface.SANS_SERIF);LrcViewThread = new Thread(new LrcViewDisplay());LrcViewThread.start();LrcViewHandler = new Handler();mRunnable = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubinvalidate();}};}/*** After constracting, you must add data using this method, or display empty* * @author zhangdong* @param MilliSecond* lryc line display time(ms)* @param lrcline* lryc line display text* @return true:success; false:fail* */public boolean LrcViewAddData(long MilliSecond, String lrcline) {if (lrcline != null) {LrcViewData linedata = new LrcViewData();linedata.MilliSecond = MilliSecond;linedata.LrcLine = lrcline;ListViewData.add(linedata);return true;}return false;}/*** Using this method when you not sure lryc data list is right or wrong* * @author zhangdong* @param null* @return null* */public void LrcViewPrintList() {for (int i = 0; i < ListViewData.size(); i++) {LrcViewData linedata = (LrcViewData) ListViewData.get(i);System.out.println(">>>" + linedata.MilliSecond + ">>>>"+ linedata.LrcLine);}}/*** start display lryc when you want to* * @author CoCo.Dota* @param null* @return null* */public void LrcViewStart() {if (startorpause != true) {startorpause = true;}}/*** Pause display lryc when you want to* * @author zhangdong* @param null* @return null* */public void LrcViewPause() {if (startorpause != false) {startorpause = false;}}public void LrcViewReset() {if (startorpause != false) {startorpause = false;}line = 0;ListViewData.clear();}// Lryc Display Threadprivate class LrcViewDisplay implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stub// remember previous line lryc time(ms)long time = 0;while (true) {if (startorpause == true) {if (!ListViewData.isEmpty()) {LrcViewData LineData = (LrcViewData) ListViewData.get(line);if (time >= LineData.MilliSecond) {// if time is up// update display current lineLrcViewHandler.post(mRunnable);// get next lineline++;} else {try {Thread.sleep(LineData.MilliSecond - time);// remember previous line lryc time(ms)time = LineData.MilliSecond;} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} else {try {Thread.sleep(500);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}protected void onSizeChanged(int w, int h, int ow, int oh) {super.onSizeChanged(w, h, ow, oh);mX = w * 0.5f; // remember the center of the screenmY = h;middleY = h * 0.5f;}protected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint p = normal;Paint p2 = highlight;p.setTextAlign(Paint.Align.CENTER);p2.setTextAlign(Paint.Align.CENTER);LrcViewData lrcviewdata = (LrcViewData) ListViewData.get(line);String linelrc = lrcviewdata.LrcLine;float tempY = middleY;// draw current linecanvas.drawText(linelrc, mX, tempY, p2);// draw previours linesfor (int i = line - 1; i >= 0; i--) {tempY = tempY - DY;if (tempY < 0) {break;}lrcviewdata = (LrcViewData) ListViewData.get(i);linelrc = lrcviewdata.LrcLine;canvas.drawText(linelrc, mX, tempY, p);}tempY = middleY;// draw lines after current linefor (int i = line + 1; i < ListViewData.size(); i++) {tempY = tempY + DY;if (tempY > mY) {break;}lrcviewdata = (LrcViewData) ListViewData.get(i);linelrc = lrcviewdata.LrcLine;canvas.drawText(linelrc, mX, tempY, p);}}
}
很好理解,开辟一个和UI无关的线程,控制歌词显示的时间,到时通过handler的post更新UI界面,实现滚动显示歌词,但是歌词滚动不平滑。
本人android初学者,有错误还请大家多多指出^_^。
这篇关于android歌词显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!