将前面的demo修改了些做个了view,可以设置字体颜色,typeface, 大小 ,行距,背景颜色等。

代码如下:

package ru.org.piaozhiye.lyric; import java.util.List; 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; /** * @author piaozhiye * */ public class LyricView extends TextView { private Paint NotCurrrentPaint; // 非当前歌词画笔 private Paint CurrentPaint; // 当前歌词画笔 private int notCurrrentPaintColor = Color.WHITE;// 非当前歌词画笔 颜色 private int CurrrentPaintColor = Color.RED; // 当前歌词画笔 颜色 private Typeface Texttypeface = Typeface.SERIF; private float width; private static Lyric mLyric; private int brackgroundcolor = 0xff000000; // 背景颜色 private float lrcTextSize = 20; // 歌词大小 // private Align = Paint.Align.CENTER; public float mTouchHistoryY; private int height; private long currentDunringTime; // 当前行歌词持续的时间,用该时间来sleep // private float middleY;// y轴中间 private int TextHeight = 50; // 每一行的间隔 public int index = 0; private List<Sentence> Sentencelist; // 歌词列表 public Paint getNotCurrrentPaint() { return NotCurrrentPaint; } public void setNotCurrrentPaint(Paint notCurrrentPaint) { NotCurrrentPaint = notCurrrentPaint; } public float getLrcTextSize() { return lrcTextSize; } public void setLrcTextSize(float lrcTextSize) { this.lrcTextSize = lrcTextSize; } public static Lyric getmLyric() { return mLyric; } public void setmLyric(Lyric mLyric) { LyricView.mLyric = mLyric; } public Paint getCurrentPaint() { return CurrentPaint; } public void setCurrentPaint(Paint currentPaint) { CurrentPaint = currentPaint; } public List<Sentence> getSentencelist() { return Sentencelist; } public void setSentencelist(List<Sentence> sentencelist) { Sentencelist = sentencelist; } public int getNotCurrrentPaintColor() { return notCurrrentPaintColor; } public void setNotCurrrentPaintColor(int notCurrrentPaintColor) { this.notCurrrentPaintColor = notCurrrentPaintColor; } public int getCurrrentPaintColor() { return CurrrentPaintColor; } public void setCurrrentPaintColor(int currrentPaintColor) { CurrrentPaintColor = currrentPaintColor; } public Typeface getTexttypeface() { return Texttypeface; } public void setTexttypeface(Typeface texttypeface) { Texttypeface = texttypeface; } public int getBrackgroundcolor() { return brackgroundcolor; } public void setBrackgroundcolor(int brackgroundcolor) { this.brackgroundcolor = brackgroundcolor; } public int getTextHeight() { return TextHeight; } public void setTextHeight(int textHeight) { TextHeight = textHeight; } public LyricView(Context context) { super(context); init(); } public LyricView(Context context, AttributeSet attr) { super(context, attr); init(); } public LyricView(Context context, AttributeSet attr, int i) { super(context, attr, i); init(); } private void init() { setFocusable(true); // PlayListItem pli = new PlayListItem("Because Of You", // "/sdcard/MP3/Because Of You.mp3", 0L, true); // mLyric = new Lyric(new File("/sdcard/MP3/Because Of You.lrc"), pli); // 非高亮部分 NotCurrrentPaint = new Paint(); NotCurrrentPaint.setAntiAlias(true); NotCurrrentPaint.setTextSize(lrcTextSize); // NotCurrrentPaint.setColor(notCurrrentPaintColor); NotCurrrentPaint.setTypeface(Texttypeface); NotCurrrentPaint.setTextAlign(Paint.Align.CENTER); // 高亮部分 当前歌词 CurrentPaint = new Paint(); CurrentPaint.setAntiAlias(true); // CurrentPaint.setColor(CurrrentPaintColor); CurrentPaint.setTextSize(lrcTextSize); CurrentPaint.setTypeface(Texttypeface); CurrentPaint.setTextAlign(Paint.Align.CENTER); // list = mLyric.list; } protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(brackgroundcolor); NotCurrrentPaint.setColor(notCurrrentPaintColor); CurrentPaint.setColor(CurrrentPaintColor); if (index == -1) return; // 先画当前行,之后再画他的前面和后面,这样就保持当前行在中间的位置 try { canvas.drawText(Sentencelist.get(index).getContent(), width / 2, height / 2, CurrentPaint); float tempY = height / 2; // 画出本句之前的句子 for (int i = index - 1; i >= 0; i--) { // Sentence sen = list.get(i); // 向上推移 tempY = tempY - TextHeight; if (tempY < 0) { break; } canvas.drawText(Sentencelist.get(i).getContent(), width / 2, tempY, NotCurrrentPaint); // canvas.translate(0, DY); } tempY = height / 2; // 画出本句之后的句子 for (int i = index + 1; i < Sentencelist.size(); i++) { // 往下推移 tempY = tempY + TextHeight; if (tempY > height) { break; } canvas.drawText(Sentencelist.get(i).getContent(), width / 2, tempY, NotCurrrentPaint); // canvas.translate(0, DY); } } catch (Exception ex) { ex.printStackTrace(); } } protected void onSizeChanged(int w, int h, int ow, int oh) { super.onSizeChanged(w, h, ow, oh); width = w; // remember the center of the screen height = h; // middleY = h * 0.5f; } // /** * @param time * 当前歌词的时间轴 * * @return currentDunringTime 歌词只需的时间 */ public long updateIndex(long time) { // 歌词序号 index = mLyric.getNowSentenceIndex(time); if (index == -1) return -1; Sentence sen = Sentencelist.get(index); // 返回歌词持续的时间,在这段时间内sleep return currentDunringTime = sen.getDuring(); } }

下面是在activity里面的使用,应该还有bug的。

package ru.org.piaozhiye; import java.io.File; import java.io.IOException; import ru.org.piaozhiye.lyric.Lyric; import ru.org.piaozhiye.lyric.LyricView; import ru.org.piaozhiye.lyric.PlayListItem; import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; public class LyricDemo extends Activity { private MediaPlayer mp; private LyricView lyricView; private String path = "/sdcard/MP3/Because Of You.mp3"; public static Lyric mLyric; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lyricView = (LyricView) findViewById(R.id.audio_lrc); mp = new MediaPlayer(); mp.reset(); try { mp.setDataSource(path); mp.prepare(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mp.start(); PlayListItem pli = new PlayListItem("Because Of You", "/sdcard/MP3/Because Of You.mp3", 0L, true); mLyric = new Lyric(new File("/sdcard/MP3/Because Of You.lrc"), pli); // System.out.println("mLyric" + mLyric.list); lyricView.setmLyric(mLyric); lyricView.setSentencelist(mLyric.list); lyricView.setNotCurrrentPaintColor(Color.GREEN); lyricView.setCurrrentPaintColor(Color.BLUE); lyricView.setLrcTextSize(20); lyricView.setTexttypeface(Typeface.SANS_SERIF); lyricView.setBrackgroundcolor(Color.BLACK); lyricView.setTextHeight(40); new Thread(new UIUpdateThread()).start(); } class UIUpdateThread implements Runnable { // long time = 100; // 开始 的时间,不能为零,否则前面几句歌词没有显示出来 public void run() { while (mp.isPlaying()) { long sleeptime = lyricView .updateIndex(mp.getCurrentPosition() + 10); mHandler.post(mUpdateResults); if (sleeptime != -1) { try { Thread.sleep(sleeptime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } Handler mHandler = new Handler(); Runnable mUpdateResults = new Runnable() { public void run() { lyricView.invalidate(); // 更新视图 } }; }

project还是使用原来的project:

http://blog.csdn.net/piaozhiye/archive/2011/04/13/6320066.aspx

更多相关文章

  1. android第四天早:多线程基础
  2. Android基于Handler实现倒计时
  3. Android(安卓)自定义缩短Toast显示时间
  4. Android(安卓)列表按照时间排序
  5. Android(安卓)获取系统日期时间并且不断更新,类似时钟
  6. Android各版本代号、版本号、API/NDK级别、发布时间及市场份额
  7. Android(安卓)- 时间 日期相关组件
  8. android 双击事件的实现
  9. Android(安卓)MP3音乐播放器总结

随机推荐

  1. C# 5.0引入了两个关键字 --async和await
  2. 解决 ASP.NET Core MySql varchar 字符串
  3. 让WebAPI 返回JSON格式的数据实例教程
  4. 分享GTS-800二次开发流程步骤
  5. socket传输protobuf字节流的实例详解
  6. C# 定时器Timer的实例介绍
  7. C# Md5Hash的用法及实例
  8. 用Shape做动画实例代码
  9. httpHelper 从URL获取值的实例代码
  10. 分享PART_Editor的使用实例