Runt
2022-12-26 549a487148522fa4a459967cd6546e22a315ae52
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.auto.lyric.widgets;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
 
import com.auto.lyric.data.LyricObject;
import com.auto.lyric.data.LyricServer;
import com.auto.lyric.util.DeviceUtil;
 
/**
 * 歌词
 * Created by Runt (qingingrunt2010@qq.com) on 2022/4/30.
 */
public class LyricView extends androidx.appcompat.widget.AppCompatTextView {
 
 
    private float mX;       //屏幕X轴的中点,此值固定,保持歌词在X中间显示
    private float offsetY;      //歌词在Y轴上的偏移量,此值会根据歌词的滚动变小
    private int lrcIndex=0; //保存歌词TreeMap的下标
    private  int SIZEWORD=0;//显示歌词文字的大小值
    private  int INTERVAL=45;//歌词每行的间隔
    Paint paint=new Paint();//画笔,用于画不是高亮的歌词
    Paint paintHL=new Paint();  //画笔,用于画高亮的歌词,即当前唱到这句歌词
 
    public LyricView(Context context){
        super(context);
        init();
    }
 
    public LyricView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    /* (non-Javadoc)
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        if(LyricServer.getLrc_map().size() > 0){
            paintHL.setTextSize(SIZEWORD);
            paint.setTextSize(SIZEWORD);
            LyricObject temp= LyricServer.getLrc_map().get(lrcIndex);
            canvas.drawText(temp.lrc, mX, offsetY+(SIZEWORD+INTERVAL)*lrcIndex, paintHL);
            // 画当前歌词之前的歌词
            for(int i=lrcIndex-1;i>=0;i--){
                temp=LyricServer.getLrc_map().get(i);
                if(offsetY+(SIZEWORD+INTERVAL)*i<0){
                    break;
                }
                canvas.drawText(temp.lrc, mX, offsetY+(SIZEWORD+INTERVAL)*i, paint);
            }
            // 画当前歌词之后的歌词
            for(int i=lrcIndex+1;i<LyricServer.getLrc_map().size();i++){
                temp=LyricServer.getLrc_map().get(i);
                if(offsetY+(SIZEWORD+INTERVAL)*i>600){
                    break;
                }
                canvas.drawText(temp.lrc, mX, offsetY+(SIZEWORD+INTERVAL)*i, paint);
            }
        }
        else{
            paint.setTextSize(25);
            canvas.drawText("找不到歌词", mX, 310, paint);
        }
        super.onDraw(canvas);
    }
 
    public void init(){
        offsetY=320;
 
        paint=new Paint();
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setColor(Color.GREEN);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setAlpha(180);
 
 
        paintHL=new Paint();
        paintHL.setTextAlign(Paint.Align.CENTER);
 
        paintHL.setColor(Color.RED);
        paintHL.setAntiAlias(true);
        paintHL.setAlpha(255);
    }
 
    /**
     * 根据歌词里面最长的那句来确定歌词字体的大小
     */
    public void setTextSize(){
        if(LyricServer.getLrc_map().size() == 0){
            return;
        }
        int max=LyricServer.getLrc_map().get(0).lrc.length();
        for(int i=1;i<LyricServer.getLrc_map().size();i++){
            LyricObject lrcStrLength=LyricServer.getLrc_map().get(i);
            if(max<lrcStrLength.lrc.length()){
                max=lrcStrLength.lrc.length();
            }
        }
        SIZEWORD = DeviceUtil.convertDpToPixel(320/max,getContext());
 
    }
 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mX = w * 0.5f;
        super.onSizeChanged(w, h, oldw, oldh);
    }
 
    /**
     *  歌词滚动的速度
     *
     * @return 返回歌词滚动的速度
     */
    public Float SpeedLrc(){
        float speed=0;
        if(offsetY+(SIZEWORD+INTERVAL)*lrcIndex>220){
            speed=((offsetY+(SIZEWORD+INTERVAL)*lrcIndex-220)/20);
 
        } else if(offsetY+(SIZEWORD+INTERVAL)*lrcIndex < 120){
            Log.i("speed", "speed is too fast!!!");
            speed = 0;
        }
        //      if(speed<0.2){
        //          speed=0.2f;
        //      }
        return speed;
    }
 
    /**
     * 按当前的歌曲的播放时间,从歌词里面获得那一句
     * @param time 当前歌曲的播放时间
     * @return 返回当前歌词的索引值
     */
    public int getIndex(int time){
        int index=0;
        for(int i=0;i<LyricServer.getLrc_map().size();i++){
            LyricObject temp=LyricServer.getLrc_map().get(i);
            if(temp.begintime<time){
                ++index;
            }
        }
        lrcIndex = index-1;
        if(lrcIndex<0){
            lrcIndex=0;
        }
        return lrcIndex;
    }
 
    public void setIndex(int lrcIndex) {
        this.lrcIndex = lrcIndex;
    }
 
    /**
     * @return the offsetY
     */
    public float getOffsetY() {
        return offsetY;
    }
 
    /**
     * @param offsetY the offsetY to set
     */
    public void setOffsetY(float offsetY) {
        this.offsetY = offsetY;
    }
 
    /**
     * @return 返回歌词文字的大小
     */
    public int getSIZEWORD() {
        return SIZEWORD;
    }
 
    /**
     * 设置歌词文字的大小
     * @param sIZEWORD the sIZEWORD to set
     */
    public void setSIZEWORD(int sIZEWORD) {
        SIZEWORD = sIZEWORD;
    }
 
    public int getIndexFromOffsetY(float offsetY){
        //220 - index * (binding.lyric.getSIZEWORD() + 44)
        int index = (int) ((220 - offsetY) / (SIZEWORD + 44));
        Log.i("LyricView", "getIndexFromOffsetY index:"+index);
        if(index < 0){
            return 0;
        }else if(LyricServer.getLrc_map().keySet().size()>index){
            return index;
        }else {
            return LyricServer.getLrc_map().keySet().size() -1;
        }
    }
}