轉載自:http://www.cnblogs.com/withwind318/p/5541267.html
感謝作者!!
先解釋一個類:Paint.FontMetrics,它表示繪製字體時的度量標準。google的官方api文檔對它的字段說明如下:
ascent: 字體最上端到基線的距離,爲負值。
descent:字體最下端到基線的距離,爲正值。
如下圖:
中間那條線就是基線,基線到上面那條線的距離就是ascent,基線到下面那條線的距離就是descent。
回到主題,我們要讓imagespan與text對齊,只需把imagespan放到descent線和ascent線之間的中間位置就可以了。實現方式爲重寫ImageSpan類的draw方法。最終實現方法如下:
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
int start, int end, float x,
int top, int y, int bottom, @NonNull Paint paint) {
// image to draw
Drawable b = getDrawable();
// font metrics of text to be replaced
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
int transY = (y + fm.descent + y + fm.ascent) / 2
- b.getBounds().bottom / 2;
canvas.save();
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
解釋下形參:
x,要繪製的image的左邊框到textview左邊框的距離。
y,要替換的文字的基線座標,即基線到textview上邊框的距離。
top,替換行的最頂部位置。
bottom,替換行的最底部位置。注意,textview中兩行之間的行間距是屬於上一行的,所以這裏bottom是指行間隔的底部位置。
paint,畫筆,包含了要繪製字體的度量信息。
這幾個參數含義在代碼中找不到說明,寫了個demo測出來的。top和bottom參數只是解釋下,函數裏面用不上。
然後解釋下代碼邏輯:
getDrawable獲取要繪製的image,getBounds是獲取包裹image的矩形框尺寸;
y + fm.descent得到字體的descent線座標;
y + fm.ascent得到字體的ascent線座標;
兩者相加除以2就是兩條線中線的座標;
b.getBounds().bottom是image的高度(試想把image放到原點),除以2即高度一半;
前面得到的中線座標減image高度的一半就是image頂部要繪製的目標位置;
最後把目標座標傳遞給canvas.translate函數就可以了,至於這個函數的理解先不管了。
原理上大致就這樣了,最後提供本文提出問題的最終解決方案,使用自定義的ImageSpan類,只需重寫它的draw函數,代碼如下:
public class CenteredImageSpan extends ImageSpan {
public CenteredImageSpan(Context context, final int drawableRes) {
super(context, drawableRes);
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
int start, int end, float x,
int top, int y, int bottom, @NonNull Paint paint) {
// image to draw
Drawable b = getDrawable();
// font metrics of text to be replaced
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
int transY = (y + fm.descent + y + fm.ascent) / 2
- b.getBounds().bottom / 2;
canvas.save();
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
}
釋放終極版本:
上面版本的ImageSpan有的手機或者換行會有效果差異,現在送上終極版本:
public class VerticalImageSpan extends ImageSpan {
public VerticalImageSpan(Drawable drawable) {
super(drawable);
}
/**
* update the text line height
*/
@Override
public int getSize(Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fontMetricsInt) {
Drawable drawable = getDrawable();
Rect rect = drawable.getBounds();
if (fontMetricsInt != null) {
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.descent - fmPaint.ascent;
int drHeight = rect.bottom - rect.top;
int centerY = fmPaint.ascent + fontHeight / 2;
fontMetricsInt.ascent = centerY - drHeight / 2;
fontMetricsInt.top = fontMetricsInt.ascent;
fontMetricsInt.bottom = centerY + drHeight / 2;
fontMetricsInt.descent = fontMetricsInt.bottom;
}
return rect.right;
}
/**
* see detail message in android.text.TextLine
*
* @param canvas the canvas, can be null if not rendering
* @param text the text to be draw
* @param start the text start position
* @param end the text end position
* @param x the edge of the replacement closest to the leading margin
* @param top the top of the line
* @param y the baseline
* @param bottom the bottom of the line
* @param paint the work paint
*/
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
Drawable drawable = getDrawable();
canvas.save();
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.descent - fmPaint.ascent;
int centerY = y + fmPaint.descent - fontHeight / 2;
int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
}
感謝博主!!!原文鏈接:http://www.cnblogs.com/withwind318/p/5541267.html