Android实现歌词滚动效果

作者:书弋江山 时间:2023-11-08 04:29:54 

本文实例为大家分享了Android实现歌词滚动效果的具体代码,供大家参考,具体内容如下

自定义TextView


public class VerticalScrollTextView extends TextView {
private Paint mPaint;
private float mX;
private Paint mPathPaint;
public int index = 0;
private List<Sentence> list;
public float mTouchHistoryY;
private int mY;
private float middleY;//
private static final int DY = 40; //
public VerticalScrollTextView(Context context) {
super(context);
init();
}
public VerticalScrollTextView(Context context, AttributeSet attr) {
super(context, attr);
init();
}
public VerticalScrollTextView(Context context, AttributeSet attr, int i) {
super(context, attr, i);
init();
}
private void init() {
setFocusable(true);
if(list==null){
 list=new ArrayList<Sentence>();
 Sentence sen=new Sentence(0," ");
 list.add(0, sen);
}

//
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(24);
mPaint.setColor(Color.BLACK);
mPaint.setAlpha(80);
mPaint.setTypeface(Typeface.SERIF);

//
mPathPaint = new Paint();
mPathPaint.setAntiAlias(true);
mPathPaint.setColor(Color.RED);
mPathPaint.setTextSize(24);
mPathPaint.setTypeface(Typeface.SANS_SERIF);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(0xEFeffff);
Paint p = mPaint;
Paint p2 = mPathPaint;
p.setTextAlign(Paint.Align.LEFT);
if (index == -1)
 return;
p2.setTextAlign(Paint.Align.LEFT);
//
canvas.drawText(list.get(index).getName(), mX, middleY, p2);
float tempY = middleY;
//
for (int i = index - 1; i >= 0; i--) {  
 tempY = tempY - DY;
 if (tempY < 0) {
 break;
 }
 canvas.drawText(list.get(i).getName(), mX, tempY, p);  
}
tempY = middleY;
//
for (int i = index + 1; i < list.size(); i++) {
 //
 tempY = tempY + DY;
 if (tempY > mY) {
 break;
 }
 canvas.drawText(list.get(i).getName(), mX, tempY, p);  
}
}
protected void onSizeChanged(int w, int h, int ow, int oh) {
super.onSizeChanged(w, h, ow, oh);
mX = w * 0.3f;
mY = h;
middleY = h * 0.5f;
}

public long updateIndex(int index) {
if (index == -1)
 return -1;
this.index=index;
return index;
}

public List<Sentence> getList() {
return list;
}

public void setList(List<Sentence> list) {
this.list = list;
}
public void updateUI(){
new Thread(new updateThread()).start();
}
class updateThread implements Runnable {
long time = 300;
int i=0;
public void run() {
 while (true) {
 long sleeptime = updateIndex(i);
 time += sleeptime;
 mHandler.post(mUpdateResults);
 if (sleeptime == -1)
  return;
 try {
  Thread.sleep(time);
  i++;
  if(i==getList().size())
  {
   i=0;
   time = 300;
  }
 } catch (InterruptedException e) {  
  e.printStackTrace();
 }
 }
}
}
Handler mHandler = new Handler();
Runnable mUpdateResults = new Runnable() {
public void run() {
 invalidate(); //
}
};
}

数据封装类


public class Sentence {

private String name;
private int index;

public Sentence(int index,String name){
this.name=name;
this.index=index;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

}

布局


<com.mypackager.ui.VerticalScrollTextView
 android:id="@+id/scoll_textView"
 android:layout_width="500dp"
 android:layout_height="500dp"
 android:text="@string/company_intrduce_text"
 android:visibility="gone"
 ></com.mypackager.VerticalScrollTextView>

Activity代码


List lst=new ArrayList<Sentence>();
 for(int i=0;i<8;i++){
 if(i%2==0){
  Sentence sen=new Sentence(i,i+1+"NanJINGXIXI");
  lst.add(i, sen);
 }else{
  Sentence sen=new Sentence(i,i+1+"Hello world!");
  lst.add(i, sen);
 }
 }

play_textView.setList(lst);

play_textView.updateUI();

来源:https://blog.csdn.net/bobbob32/article/details/72833686

标签:Android,歌词滚动
0
投稿

猜你喜欢

  • 利用Jackson解决Json序列化和反序列化问题

    2023-02-16 14:59:54
  • JSch教程使用sftp协议实现服务器文件载操作

    2023-10-29 17:43:33
  • 如何在C#9 中使用顶级程序 (top-level)

    2023-08-20 04:13:24
  • springboot如何读取自定义配置项

    2021-06-18 10:44:53
  • Java如何利用状态模式(state pattern)替代if else

    2021-08-15 09:31:08
  • Java 设计模式中的策略模式详情

    2023-08-06 03:45:11
  • C#实现添加多行文本水印到Word文档

    2023-03-22 07:45:33
  • 基于C#实现XML文件读取工具类

    2021-10-07 07:42:19
  • Android植物大战僵尸小游戏

    2023-08-05 21:27:04
  • Android 仿淘宝、京东商品详情页向上拖动查看图文详情控件DEMO详解

    2022-01-17 12:21:18
  • Android使用kotlin实现多行文本上下滚动播放

    2022-05-09 08:08:29
  • asp.net core项目mvc权限控制:分配权限

    2023-09-06 04:24:13
  • 如何用120行Java代码写一个自己的区块链

    2023-07-17 03:44:33
  • java 生成xml并转为字符串的方法

    2023-01-07 08:27:30
  • C#将配置文件appsetting中的值转换为动态对象调用

    2023-11-28 01:29:53
  • java如何获取本地操作系统进程列表

    2022-12-05 13:35:20
  • C#实现带阴历显示的日期代码

    2023-12-11 02:19:04
  • mybatis-plus自动生成代码的示例代码

    2023-08-04 22:38:32
  • spring boot validation参数校验实例分析

    2023-02-03 02:50:38
  • C#中Lambda表达式的用法

    2022-12-22 15:59:17
  • asp之家 软件编程 m.aspxhome.com