我是靠谱客的博主 大胆钢笔,最近开发中收集的这篇文章主要介绍Android实现歌词滚动效果,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

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

最后

以上就是大胆钢笔为你收集整理的Android实现歌词滚动效果的全部内容,希望文章能够帮你解决Android实现歌词滚动效果所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(103)

评论列表共有 0 条评论

立即
投稿
返回
顶部