36
36
import android .text .style .ClickableSpan ;
37
37
import android .text .style .ForegroundColorSpan ;
38
38
import android .text .style .LeadingMarginSpan ;
39
+ import android .text .style .LineHeightSpan ;
39
40
import android .text .style .MaskFilterSpan ;
40
41
import android .text .style .RelativeSizeSpan ;
41
42
import android .text .style .ReplacementSpan ;
@@ -835,6 +836,7 @@ public SpanUtils appendImage(@DrawableRes final int resourceId) {
835
836
* @return {@link SpanUtils}
836
837
*/
837
838
public SpanUtils appendImage (@ DrawableRes final int resourceId , @ Align final int align ) {
839
+ append (Character .toString ((char ) 0 ));
838
840
apply (mTypeImage );
839
841
this .imageResourceId = resourceId ;
840
842
this .alignImage = align ;
@@ -1194,7 +1196,7 @@ public void drawLeadingMargin(final Canvas c, final Paint p, final int x, final
1194
1196
}
1195
1197
}
1196
1198
1197
- class CustomIconMarginSpan implements LeadingMarginSpan , android . text . style . LineHeightSpan {
1199
+ class CustomIconMarginSpan implements LeadingMarginSpan , LineHeightSpan {
1198
1200
Bitmap mBitmap ;
1199
1201
1200
1202
static final int ALIGN_CENTER = 2 ;
@@ -1479,21 +1481,29 @@ private CustomDynamicDrawableSpan(final int verticalAlignment) {
1479
1481
1480
1482
@ Override
1481
1483
public int getSize (@ NonNull final Paint paint , final CharSequence text ,
1482
- final int start , final int end ,
1483
- final Paint .FontMetricsInt fm ) {
1484
+ final int start , final int end , final Paint .FontMetricsInt fm ) {
1484
1485
Drawable d = getCachedDrawable ();
1485
1486
Rect rect = d .getBounds ();
1486
- final int fontHeight = (int ) (paint .getFontMetrics ().descent - paint .getFontMetrics ().ascent );
1487
- if (fm != null ) { // this is the fucking code which I waste 3 days
1488
- if (rect .height () > fontHeight ) {
1487
+ if (fm != null ) {
1488
+ LogUtils .d ("fm.top: " + fm .top ,
1489
+ "fm.ascent: " + fm .ascent ,
1490
+ "fm.descent: " + fm .descent ,
1491
+ "fm.bottom: " + fm .bottom ,
1492
+ "lineHeight: " + (fm .bottom - fm .top ));
1493
+ int lineHeight = fm .bottom - fm .top ;
1494
+ if (lineHeight < rect .height ()) {
1489
1495
if (mVerticalAlignment == ALIGN_TOP ) {
1490
- fm .descent += rect .height () - fontHeight ;
1496
+ fm .top = fm .top ;
1497
+ fm .bottom = rect .height () + fm .top ;
1491
1498
} else if (mVerticalAlignment == ALIGN_CENTER ) {
1492
- fm .ascent -= ( rect .height () - fontHeight ) / 2 ;
1493
- fm .descent += ( rect .height () - fontHeight ) / 2 ;
1499
+ fm .top = - rect .height () / 2 - lineHeight / 4 ;
1500
+ fm .bottom = rect .height () / 2 - lineHeight / 4 ;
1494
1501
} else {
1495
- fm .ascent -= rect .height () - fontHeight ;
1502
+ fm .top = -rect .height () + fm .bottom ;
1503
+ fm .bottom = fm .bottom ;
1496
1504
}
1505
+ fm .ascent = fm .top ;
1506
+ fm .descent = fm .bottom ;
1497
1507
}
1498
1508
}
1499
1509
return rect .right ;
@@ -1506,18 +1516,25 @@ public void draw(@NonNull final Canvas canvas, final CharSequence text,
1506
1516
Drawable d = getCachedDrawable ();
1507
1517
Rect rect = d .getBounds ();
1508
1518
canvas .save ();
1509
- final float fontHeight = paint .getFontMetrics ().descent - paint .getFontMetrics ().ascent ;
1510
- int transY = bottom - rect .bottom ;
1511
- if (rect .height () < fontHeight ) { // this is the fucking code which I waste 3 days
1512
- if (mVerticalAlignment == ALIGN_BASELINE ) {
1513
- transY -= paint .getFontMetricsInt ().descent ;
1519
+ float transY ;
1520
+ int lineHeight = bottom - top ;
1521
+ LogUtils .d ("rectHeight: " + rect .height (),
1522
+ "lineHeight: " + (bottom - top ),
1523
+ "base: " + (bottom - y ));
1524
+ if (rect .height () < lineHeight ) {
1525
+ if (mVerticalAlignment == ALIGN_TOP ) {
1526
+ transY = top ;
1514
1527
} else if (mVerticalAlignment == ALIGN_CENTER ) {
1515
- transY -= (fontHeight - rect .height ()) / 2 ;
1516
- } else if (mVerticalAlignment == ALIGN_TOP ) {
1517
- transY -= fontHeight - rect .height ();
1528
+ transY = (bottom + top - rect .height ()) / 2 ;
1529
+ } else if (mVerticalAlignment == ALIGN_BASELINE ) {
1530
+ transY = y - rect .height ();
1531
+ } else {
1532
+ transY = bottom - rect .height ();
1518
1533
}
1534
+ canvas .translate (x , transY );
1535
+ } else {
1536
+ canvas .translate (x , top );
1519
1537
}
1520
- canvas .translate (x , transY );
1521
1538
d .draw (canvas );
1522
1539
canvas .restore ();
1523
1540
}
@@ -1531,7 +1548,7 @@ private Drawable getCachedDrawable() {
1531
1548
d = getDrawable ();
1532
1549
mDrawableRef = new WeakReference <>(d );
1533
1550
}
1534
- return getDrawable () ;
1551
+ return d ;
1535
1552
}
1536
1553
1537
1554
private WeakReference <Drawable > mDrawableRef ;
0 commit comments