24
24
import android .text .style .StyleSpan ;
25
25
import android .text .style .UnderlineSpan ;
26
26
import android .util .AttributeSet ;
27
- import android .util .Log ;
28
27
import android .view .View ;
29
28
import android .view .ViewGroup ;
30
29
@@ -137,6 +136,10 @@ public void collapsed(boolean collapsed) {
137
136
rebuild = true ;
138
137
}
139
138
139
+ public boolean isCollapsed () {
140
+ return this .collapsed ;
141
+ }
142
+
140
143
@ Override
141
144
public void setText (CharSequence text , BufferType type ) {
142
145
if (isAnimate ) {
@@ -199,14 +202,14 @@ private boolean isEllipsized(String text) {
199
202
200
203
private void collapsedBuilder () {
201
204
StaticLayout layout ;
202
- String replaceSpace = textReplaceSpace ();
205
+ // String replaceSpace = textReplaceSpace();
203
206
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
204
207
layout = StaticLayout .Builder
205
- .obtain (replaceSpace , 0 , replaceSpace .length (), getPaint (), lineWidth )
208
+ .obtain (text , 0 , text .length (), getPaint (), lineWidth )
206
209
.setLineSpacing (getLineSpacingExtra (), getLineSpacingMultiplier ())
207
210
.build ();
208
211
} else {
209
- layout = new StaticLayout (replaceSpace , getPaint (), lineWidth , Layout .Alignment .ALIGN_NORMAL ,
212
+ layout = new StaticLayout (text , getPaint (), lineWidth , Layout .Alignment .ALIGN_NORMAL ,
210
213
getLineSpacingMultiplier (), getLineSpacingExtra (), true
211
214
);
212
215
}
@@ -217,7 +220,7 @@ private void collapsedBuilder() {
217
220
int count = (int ) layout .getLineWidth (i );
218
221
int start = layout .getLineStart (i );
219
222
int end = layout .getLineEnd (i );
220
- lastLineLetter = replaceSpace .subSequence (start , end );
223
+ lastLineLetter = text .subSequence (start , end );
221
224
lastLineLetterCount = lastLineLetter .length ();
222
225
sumLineWidth +=count ;
223
226
}
@@ -236,7 +239,7 @@ private void collapsedBuilder() {
236
239
if (sumLineWidth < doubleExpandWith ) {
237
240
truncatedTextWidth = sumLineWidth ;
238
241
}
239
- CharSequence truncatedText = TextUtils .ellipsize (replaceSpace , getPaint (), truncatedTextWidth , TextUtils .TruncateAt .END );
242
+ CharSequence truncatedText = TextUtils .ellipsize (text , getPaint (), truncatedTextWidth , TextUtils .TruncateAt .END );
240
243
String exp = expandText .replaceAll (" " , SPACE_CODE );
241
244
String finalText = truncatedText .toString ();
242
245
if (ellipsisType == ELLIPSIS_TYPE_NONE ) {
@@ -292,9 +295,9 @@ public void updateDrawState(@NonNull TextPaint ds) {
292
295
}
293
296
294
297
private void expandBuilder () {
295
- String replaceSpace = textReplaceSpace ();
298
+ // String replaceSpace = textReplaceSpace();
296
299
String collapsedTextSpace = collapseText .replaceAll (" " , SPACE_CODE );
297
- String fullText = replaceSpace +SPACE_CODE +collapsedTextSpace ;
300
+ String fullText = text +SPACE_CODE +collapsedTextSpace ;
298
301
spanExpanded = spanExpanded (fullText );
299
302
}
300
303
@@ -352,50 +355,51 @@ public void toggle() {
352
355
353
356
if (collapsed ) {
354
357
String collapsedTextSpace = collapseText .replaceAll (" " , SPACE_CODE );
355
- String fullText = textReplaceSpace ()+SPACE_CODE +collapsedTextSpace ;
358
+ String fullText = text +SPACE_CODE +collapsedTextSpace ;
359
+
356
360
StaticLayout staticFull ;
357
361
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
358
362
staticFull = StaticLayout .Builder
359
363
.obtain (fullText , 0 , fullText .length (), getPaint (), lineWidth )
360
364
.setLineSpacing (getLineSpacingExtra (), getLineSpacingMultiplier ())
365
+ .setIncludePad (getIncludeFontPadding ())
361
366
.build ();
362
367
} else {
363
368
staticFull = new StaticLayout (
364
369
fullText , getPaint (), lineWidth , Layout .Alignment .ALIGN_NORMAL ,
365
- getLineSpacingMultiplier (), getLineSpacingExtra (), true
370
+ getLineSpacingMultiplier (), getLineSpacingExtra (), getIncludeFontPadding ()
366
371
);
367
372
}
368
- fullHeight = staticFull .getHeight () + getCompoundPaddingTop () + getCompoundPaddingBottom ();
373
+ fullHeight = staticFull .getHeight () + getPaddingTop () + getPaddingBottom ();
369
374
} else {
370
375
StaticLayout staticHalf ;
371
376
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
372
377
staticHalf = StaticLayout .Builder
373
- .obtain (textReplaceSpace () , 0 , textReplaceSpace () .length (), getPaint (), lineWidth )
378
+ .obtain (text , 0 , text .length (), getPaint (), lineWidth )
374
379
.setMaxLines (maxLines )
375
380
.setEllipsize (TextUtils .TruncateAt .END )
376
381
.setLineSpacing (getLineSpacingExtra (), getLineSpacingMultiplier ())
382
+ .setIncludePad (getIncludeFontPadding ())
377
383
.build ();
378
384
} else {
379
385
int maxLength = text .length ();
380
386
do {
381
387
staticHalf = new StaticLayout (
382
388
ellipsize (text , maxLength ), getPaint (), lineWidth , Layout .Alignment .ALIGN_NORMAL ,
383
- getLineSpacingMultiplier (), getLineSpacingExtra (), true
389
+ getLineSpacingMultiplier (), getLineSpacingExtra (), getIncludeFontPadding ()
384
390
);
385
391
maxLength -= 10 ;
386
392
} while (staticHalf .getLineCount () > 2 );
387
393
}
388
- halfHeight = staticHalf .getHeight () + getCompoundPaddingTop () + getCompoundPaddingBottom ();
394
+ halfHeight = staticHalf .getHeight () + getPaddingTop () + getPaddingTop ();
389
395
}
390
-
391
- //int start = collapsed ? halfHeight : fullHeight;
392
- int start = getHeight ();
393
396
int end = collapsed ? fullHeight : halfHeight ;
394
- ValueAnimator anim = ValueAnimator .ofInt (start , end );
397
+ ValueAnimator anim = ValueAnimator .ofInt (getHeight () , end );
395
398
anim .setDuration (animationDuration );
396
399
ViewGroup .LayoutParams params = getLayoutParams ();
397
400
anim .addUpdateListener (animation -> {
398
- params .height = (int ) animation .getAnimatedValue ();
401
+ Object value = animation .getAnimatedValue ();
402
+ params .height = (int ) value ;
399
403
setLayoutParams (params );
400
404
});
401
405
anim .addListener (new AnimatorListenerAdapter () {
@@ -404,8 +408,7 @@ public void onAnimationStart(Animator animation) {
404
408
super .onAnimationStart (animation );
405
409
isAnimate = true ;
406
410
if (collapsed ) {
407
- String replaceSpace = textReplaceSpace ();
408
- setText (replaceSpace , BufferType .SPANNABLE );
411
+ setText (text , BufferType .SPANNABLE );
409
412
}
410
413
}
411
414
@ Override
@@ -461,17 +464,14 @@ private String ellipsize(String text, int size) {
461
464
}
462
465
}
463
466
467
+ /* Unused function
464
468
private void debug(String message) {
465
469
Log.d("TextViewReadMore", message);
466
470
}
467
-
468
- public interface ToggleListener {
469
- public void onToggle (boolean collapsed );
470
- }
471
-
472
471
private String textReplaceSpace() {
473
472
return text.replaceAll(" ", SPACE_CODE);
474
473
}
474
+ */
475
475
476
476
private void setWrapLayout () {
477
477
ViewGroup .LayoutParams params = getLayoutParams ();
0 commit comments