android歌词显示

2024-06-23 06:18
文章标签 android 显示 歌词

本文主要是介绍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歌词显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086365

相关文章

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

idea中project的显示问题及解决

《idea中project的显示问题及解决》:本文主要介绍idea中project的显示问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录idea中project的显示问题清除配置重China编程新生成配置总结idea中project的显示问题新建空的pr