3636import android .text .style .ClickableSpan ;
3737import android .text .style .ForegroundColorSpan ;
3838import android .text .style .LeadingMarginSpan ;
39+ import android .text .style .LineHeightSpan ;
3940import android .text .style .MaskFilterSpan ;
4041import android .text .style .RelativeSizeSpan ;
4142import android .text .style .ReplacementSpan ;
@@ -835,6 +836,7 @@ public SpanUtils appendImage(@DrawableRes final int resourceId) {
835836 * @return {@link SpanUtils}
836837 */
837838 public SpanUtils appendImage (@ DrawableRes final int resourceId , @ Align final int align ) {
839+ append (Character .toString ((char ) 0 ));
838840 apply (mTypeImage );
839841 this .imageResourceId = resourceId ;
840842 this .alignImage = align ;
@@ -1194,7 +1196,7 @@ public void drawLeadingMargin(final Canvas c, final Paint p, final int x, final
11941196 }
11951197 }
11961198
1197- class CustomIconMarginSpan implements LeadingMarginSpan , android . text . style . LineHeightSpan {
1199+ class CustomIconMarginSpan implements LeadingMarginSpan , LineHeightSpan {
11981200 Bitmap mBitmap ;
11991201
12001202 static final int ALIGN_CENTER = 2 ;
@@ -1479,21 +1481,29 @@ private CustomDynamicDrawableSpan(final int verticalAlignment) {
14791481
14801482 @ Override
14811483 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 ) {
14841485 Drawable d = getCachedDrawable ();
14851486 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 ()) {
14891495 if (mVerticalAlignment == ALIGN_TOP ) {
1490- fm .descent += rect .height () - fontHeight ;
1496+ fm .top = fm .top ;
1497+ fm .bottom = rect .height () + fm .top ;
14911498 } 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 ;
14941501 } else {
1495- fm .ascent -= rect .height () - fontHeight ;
1502+ fm .top = -rect .height () + fm .bottom ;
1503+ fm .bottom = fm .bottom ;
14961504 }
1505+ fm .ascent = fm .top ;
1506+ fm .descent = fm .bottom ;
14971507 }
14981508 }
14991509 return rect .right ;
@@ -1506,18 +1516,25 @@ public void draw(@NonNull final Canvas canvas, final CharSequence text,
15061516 Drawable d = getCachedDrawable ();
15071517 Rect rect = d .getBounds ();
15081518 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 ;
15141527 } 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 ();
15181533 }
1534+ canvas .translate (x , transY );
1535+ } else {
1536+ canvas .translate (x , top );
15191537 }
1520- canvas .translate (x , transY );
15211538 d .draw (canvas );
15221539 canvas .restore ();
15231540 }
@@ -1531,7 +1548,7 @@ private Drawable getCachedDrawable() {
15311548 d = getDrawable ();
15321549 mDrawableRef = new WeakReference <>(d );
15331550 }
1534- return getDrawable () ;
1551+ return d ;
15351552 }
15361553
15371554 private WeakReference <Drawable > mDrawableRef ;
0 commit comments