29
29
* Created by wcy on 2015/11/9.
30
30
*/
31
31
public class LrcView extends View {
32
- private List <Entry > mEntryList = new ArrayList <>();
32
+ private List <LrcEntry > mLrcEntryList = new ArrayList <>();
33
33
private TextPaint mPaint = new TextPaint ();
34
34
private float mTextSize ;
35
35
private float mDividerHeight ;
@@ -58,15 +58,15 @@ public LrcView(Context context, AttributeSet attrs, int defStyleAttr) {
58
58
59
59
private void init (AttributeSet attrs ) {
60
60
TypedArray ta = getContext ().obtainStyledAttributes (attrs , R .styleable .LrcView );
61
- mTextSize = ta .getDimension (R .styleable .LrcView_lrcTextSize , sp2px (16 ));
62
- mDividerHeight = ta .getDimension (R .styleable .LrcView_lrcDividerHeight , dp2px (24 ));
61
+ mTextSize = ta .getDimension (R .styleable .LrcView_lrcTextSize , sp2px (12 ));
62
+ mDividerHeight = ta .getDimension (R .styleable .LrcView_lrcDividerHeight , dp2px (16 ));
63
63
mAnimationDuration = ta .getInt (R .styleable .LrcView_lrcAnimationDuration , 1000 );
64
64
mAnimationDuration = mAnimationDuration < 0 ? 1000 : mAnimationDuration ;
65
65
mNormalColor = ta .getColor (R .styleable .LrcView_lrcNormalTextColor , 0xFFFFFFFF );
66
66
mCurrentColor = ta .getColor (R .styleable .LrcView_lrcCurrentTextColor , 0xFFFF4081 );
67
67
mLabel = ta .getString (R .styleable .LrcView_lrcLabel );
68
68
mLabel = TextUtils .isEmpty (mLabel ) ? "暂无歌词" : mLabel ;
69
- mLrcPadding = ta .getDimension (R .styleable .LrcView_lrcPadding , dp2px ( 16 ) );
69
+ mLrcPadding = ta .getDimension (R .styleable .LrcView_lrcPadding , 0 );
70
70
ta .recycle ();
71
71
72
72
mPaint .setAntiAlias (true );
@@ -100,14 +100,14 @@ protected void onDraw(Canvas canvas) {
100
100
}
101
101
102
102
// 画当前行
103
- float currY = centerY - mEntryList .get (mCurrentLine ).getTextHeight () / 2 ;
104
- drawText (canvas , mEntryList .get (mCurrentLine ).getStaticLayout (), currY );
103
+ float currY = centerY - mLrcEntryList .get (mCurrentLine ).getTextHeight () / 2 ;
104
+ drawText (canvas , mLrcEntryList .get (mCurrentLine ).getStaticLayout (), currY );
105
105
106
106
// 画当前行上面的
107
107
mPaint .setColor (mNormalColor );
108
108
float upY = currY ;
109
109
for (int i = mCurrentLine - 1 ; i >= 0 ; i --) {
110
- upY -= mDividerHeight + mEntryList .get (i ).getTextHeight ();
110
+ upY -= mDividerHeight + mLrcEntryList .get (i ).getTextHeight ();
111
111
112
112
if (mAnimator == null || !mAnimator .isStarted ()) {
113
113
// 动画已经结束,超出屏幕停止绘制
@@ -116,7 +116,7 @@ protected void onDraw(Canvas canvas) {
116
116
}
117
117
}
118
118
119
- drawText (canvas , mEntryList .get (i ).getStaticLayout (), upY );
119
+ drawText (canvas , mLrcEntryList .get (i ).getStaticLayout (), upY );
120
120
121
121
// 动画未结束,超出屏幕多绘制一行
122
122
if (upY < 0 ) {
@@ -125,23 +125,23 @@ protected void onDraw(Canvas canvas) {
125
125
}
126
126
127
127
// 画当前行下面的
128
- float downY = currY + mEntryList .get (mCurrentLine ).getTextHeight () + mDividerHeight ;
129
- for (int i = mCurrentLine + 1 ; i < mEntryList .size (); i ++) {
128
+ float downY = currY + mLrcEntryList .get (mCurrentLine ).getTextHeight () + mDividerHeight ;
129
+ for (int i = mCurrentLine + 1 ; i < mLrcEntryList .size (); i ++) {
130
130
if (mAnimator == null || !mAnimator .isStarted ()) {
131
131
// 动画已经结束,超出屏幕停止绘制
132
- if (downY + mEntryList .get (i ).getTextHeight () > getHeight ()) {
132
+ if (downY + mLrcEntryList .get (i ).getTextHeight () > getHeight ()) {
133
133
break ;
134
134
}
135
135
}
136
136
137
- drawText (canvas , mEntryList .get (i ).getStaticLayout (), downY );
137
+ drawText (canvas , mLrcEntryList .get (i ).getStaticLayout (), downY );
138
138
139
139
// 动画未结束,超出屏幕多绘制一行
140
- if (downY + mEntryList .get (i ).getTextHeight () > getHeight ()) {
140
+ if (downY + mLrcEntryList .get (i ).getTextHeight () > getHeight ()) {
141
141
break ;
142
142
}
143
143
144
- downY += mEntryList .get (i ).getTextHeight () + mDividerHeight ;
144
+ downY += mLrcEntryList .get (i ).getTextHeight () + mDividerHeight ;
145
145
}
146
146
}
147
147
@@ -230,15 +230,15 @@ public void updateTime(long time) {
230
230
if (time < mNextTime ) {
231
231
return ;
232
232
}
233
- for (int i = mCurrentLine ; i < mEntryList .size (); i ++) {
234
- if (mEntryList .get (i ).getTime () > time ) {
235
- mNextTime = mEntryList .get (i ).getTime ();
233
+ for (int i = mCurrentLine ; i < mLrcEntryList .size (); i ++) {
234
+ if (mLrcEntryList .get (i ).getTime () > time ) {
235
+ mNextTime = mLrcEntryList .get (i ).getTime ();
236
236
mCurrentLine = i < 1 ? 0 : i - 1 ;
237
237
newlineAnimate (i );
238
238
break ;
239
- } else if (i == mEntryList .size () - 1 ) {
239
+ } else if (i == mLrcEntryList .size () - 1 ) {
240
240
// 最后一行
241
- mCurrentLine = mEntryList .size () - 1 ;
241
+ mCurrentLine = mLrcEntryList .size () - 1 ;
242
242
mNextTime = Long .MAX_VALUE ;
243
243
newlineAnimate (i );
244
244
break ;
@@ -252,10 +252,15 @@ public void updateTime(long time) {
252
252
* @param time 指定的时间
253
253
*/
254
254
public void onDrag (long time ) {
255
- for (int i = 0 ; i < mEntryList .size (); i ++) {
256
- if (mEntryList .get (i ).getTime () > time ) {
257
- mNextTime = mEntryList .get (i ).getTime ();
258
- mCurrentLine = i < 1 ? 0 : i - 1 ;
255
+ for (int i = 0 ; i < mLrcEntryList .size (); i ++) {
256
+ if (mLrcEntryList .get (i ).getTime () > time ) {
257
+ if (i == 0 ) {
258
+ mCurrentLine = i ;
259
+ initNextTime ();
260
+ } else {
261
+ mCurrentLine = i - 1 ;
262
+ mNextTime = mLrcEntryList .get (i ).getTime ();
263
+ }
259
264
newlineAnimate (i );
260
265
break ;
261
266
}
@@ -268,11 +273,11 @@ public void onDrag(long time) {
268
273
* @return true,如果歌词有效,否则false
269
274
*/
270
275
public boolean hasLrc () {
271
- return !mEntryList .isEmpty ();
276
+ return !mLrcEntryList .isEmpty ();
272
277
}
273
278
274
279
private void reset () {
275
- mEntryList .clear ();
280
+ mLrcEntryList .clear ();
276
281
mCurrentLine = 0 ;
277
282
mNextTime = 0L ;
278
283
}
@@ -282,14 +287,14 @@ private void initEntryList() {
282
287
return ;
283
288
}
284
289
285
- for (Entry entry : mEntryList ) {
286
- entry .init (mPaint , (int ) getLrcWidth ());
290
+ for (LrcEntry lrcEntry : mLrcEntryList ) {
291
+ lrcEntry .init (mPaint , (int ) getLrcWidth ());
287
292
}
288
293
}
289
294
290
295
private void initNextTime () {
291
- if (mEntryList .size () > 1 ) {
292
- mNextTime = mEntryList .get (1 ).getTime ();
296
+ if (mLrcEntryList .size () > 1 ) {
297
+ mNextTime = mLrcEntryList .get (1 ).getTime ();
293
298
} else {
294
299
mNextTime = Long .MAX_VALUE ;
295
300
}
@@ -314,8 +319,8 @@ private void parseLine(String line) {
314
319
315
320
long time = min * DateUtils .MINUTE_IN_MILLIS + sec * DateUtils .SECOND_IN_MILLIS + mil * 10 ;
316
321
317
- Entry entry = new Entry (time , text );
318
- mEntryList .add (entry );
322
+ LrcEntry lrcEntry = new LrcEntry (time , text );
323
+ mLrcEntryList .add (lrcEntry );
319
324
}
320
325
321
326
/**
@@ -327,8 +332,8 @@ private void newlineAnimate(int index) {
327
332
mAnimator .end ();
328
333
}
329
334
330
- mAnimator = ValueAnimator .ofFloat (mEntryList .get (index ).getTextHeight () + mDividerHeight , 0.0f );
331
- mAnimator .setDuration (mAnimationDuration * mEntryList .get (index ).getStaticLayout ().getLineCount ());
335
+ mAnimator = ValueAnimator .ofFloat (mLrcEntryList .get (index ).getTextHeight () + mDividerHeight , 0.0f );
336
+ mAnimator .setDuration (mAnimationDuration * mLrcEntryList .get (index ).getStaticLayout ().getLineCount ());
332
337
mAnimator .addUpdateListener (new ValueAnimator .AnimatorUpdateListener () {
333
338
@ Override
334
339
public void onAnimationUpdate (ValueAnimator animation ) {
@@ -349,13 +354,13 @@ private int sp2px(float spValue) {
349
354
return (int ) (spValue * fontScale + 0.5f );
350
355
}
351
356
352
- private static class Entry {
357
+ private static class LrcEntry {
353
358
private long time ;
354
359
private String text ;
355
360
private StaticLayout staticLayout ;
356
361
private TextPaint paint ;
357
362
358
- Entry (long time , String text ) {
363
+ LrcEntry (long time , String text ) {
359
364
this .time = time ;
360
365
this .text = text ;
361
366
}
0 commit comments