Runt
2022-05-01 1c757c667d4d827cc0bcf692dae663f7ca49b01c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.auto.lyric.widgets;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;
 
import com.auto.lyric.R;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 歌词
 * Created by Runt (qingingrunt2010@qq.com) on 2022/4/30.
 */
public class LyricView extends androidx.appcompat.widget.AppCompatTextView {
    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 LyricView(Context context) {
        super(context);
        init();
    }
    public LyricView(Context context, AttributeSet attr) {
        super(context, attr);
        init();
    }
    public LyricView(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(); //
        }
    };
}