ReplacementDrawableSpanpublic class ReplacementDrawableSpan extends android.text.style.ReplacementSpan ReplacementSpan that properly draws the drawable that is centered around the text
without changing the default text size or layout. |
Fields Summary |
---|
protected android.graphics.drawable.Drawable | mDrawable | private final android.graphics.Paint | mWorkPaint | private float | mExtraMargin |
Methods Summary |
---|
public void | draw(android.graphics.Canvas canvas, java.lang.CharSequence charSequence, int start, int end, float x, int top, int y, int bottom, android.graphics.Paint paint)
canvas.save();
int transY = (bottom - mDrawable.getBounds().bottom + top) / 2;
canvas.translate(x, transY);
mDrawable.draw(canvas);
canvas.restore();
| protected android.graphics.Rect | getBounds()
return mDrawable.getBounds();
| public int | getSize(android.graphics.Paint paint, java.lang.CharSequence text, int i, int i2, Paint.FontMetricsInt fm)
setupFontMetrics(fm, paint);
return getBounds().right;
| public void | setExtraMargin(float margin)
mExtraMargin = margin;
| private void | setupFontMetrics(Paint.FontMetricsInt fm, android.graphics.Paint paint)
mWorkPaint.set(paint);
if (fm != null) {
mWorkPaint.getFontMetricsInt(fm);
final Rect bounds = getBounds();
final int textHeight = fm.descent - fm.ascent;
final int halfMargin = (int) mExtraMargin / 2;
fm.ascent = Math.min(fm.top, fm.top + (textHeight - bounds.bottom) / 2) - halfMargin;
fm.descent = Math.max(fm.bottom, fm.bottom + (bounds.bottom - textHeight) / 2)
+ halfMargin;
fm.top = fm.ascent;
fm.bottom = fm.descent;
}
|
|