歌词的获取

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="play" />

    <com.anjoyo.getlyric.ui.LyricView
        android:id="@+id/LyricShow"
        android:layout_width="fill_parent"
        android:layout_height="300dip" >
    </com.anjoyo.getlyric.ui.LyricView>

</LinearLayout>

package com.anjoyo.getlyric.ui;

import java.util.ArrayList;
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;

/**
 * 自定义TextView,用于显示歌词
 * 
 * @author anjoyo
 * 
 */
public class LyricView extends TextView {

        private float high;

        private float width;

        private Paint CurrentPaint;

        private Paint NotCurrentPaint;

        private float TextHigh = 25;

        private float TextSize = 15;

        private int Index = 0;

        private List<LyricContent> mSentenceEntities = new ArrayList<LyricContent>();

        public void setSentenceEntities(List<LyricContent> mSentenceEntities) {
                this.mSentenceEntities = mSentenceEntities;
        }

        public LyricView(Context context) {
                super(context);
                init();
                // TODO Auto-generated constructor stub
        }

        public LyricView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                init();
                // TODO Auto-generated constructor stub
        }

        public LyricView(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
                // TODO Auto-generated constructor stub
        }

        private void init() {
                setFocusable(true);

                // 高亮部分
                CurrentPaint = new Paint();
                // 去锯齿 
                CurrentPaint.setAntiAlias(true);
                CurrentPaint.setTextAlign(Paint.Align.CENTER);

                // 非高亮部分
                NotCurrentPaint = new Paint();
                NotCurrentPaint.setAntiAlias(true);
                NotCurrentPaint.setTextAlign(Paint.Align.CENTER);

        }

        @Override
        protected void onDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                super.onDraw(canvas);

                if (canvas == null) {
                        return;
                }

                CurrentPaint.setColor(Color.YELLOW);
                NotCurrentPaint.setColor(Color.GREEN);

                CurrentPaint.setTextSize(TextSize);
                CurrentPaint.setTypeface(Typeface.SERIF);

                NotCurrentPaint.setTextSize(TextSize);
                NotCurrentPaint.setTypeface(Typeface.SERIF);

                try {
                        canvas.drawText(mSentenceEntities.get(Index).getLyric(), width / 2,
                                        high / 2, CurrentPaint);

                        float tempY = high / 2;
                        // 画出本句之前的句子
                        for (int i = Index - 1; i >= 0; i--) {

                                // 向上推移
                                tempY = tempY - TextHigh;

                                canvas.drawText(mSentenceEntities.get(i).getLyric(), width / 2,
                                                tempY, NotCurrentPaint);

                        }

                        tempY = high / 2;
                        // 画出本句之后的句子
                        for (int i = Index + 1; i < mSentenceEntities.size(); i++) {
                                // 往下推移
                                tempY = tempY + TextHigh;

                                canvas.drawText(mSentenceEntities.get(i).getLyric(), width / 2,
                                                tempY, NotCurrentPaint);

                        }
                } catch (Exception e) {
                        // TODO: handle exception
                }

        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                // TODO Auto-generated method stub
                super.onSizeChanged(w, h, oldw, oldh);
                this.high = h;
                this.width = w;
        }

        public void SetIndex(int index) {
                this.Index = index;
                // System.out.println(index);
        }

}


原文地址:https://www.cnblogs.com/allencoder/p/3643918.html