欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现歌词滚动效果

程序员文章站 2023-11-09 13:33:52
本文实例为大家分享了android实现歌词滚动效果的具体代码,供大家参考,具体内容如下 自定义textview public class verticalscr...

本文实例为大家分享了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(); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。