DynamicDrawableSpan

public abstract class DynamicDrawableSpan
extends ReplacementSpan

java.lang.Object
   ↳ android.text.style.CharacterStyle
     ↳ android.text.style.MetricAffectingSpan
       ↳ android.text.style.ReplacementSpan
         ↳ android.text.style.DynamicDrawableSpan


Span that replaces the text it's attached to with a Drawable that can be aligned with the bottom or with the baseline of the surrounding text.

For an implementation that constructs the drawable from various sources (Bitmap, Drawable, resource id or Uri) use ImageSpan.

A simple implementation of DynamicDrawableSpan that uses drawables from resources looks like this:

 class MyDynamicDrawableSpan extends DynamicDrawableSpan {

 private final Context mContext;
 private final int mResourceId;

 public MyDynamicDrawableSpan(Context context, @DrawableRes int resourceId) {
     mContext = context;
     mResourceId = resourceId;
 }

 @Override
 public Drawable getDrawable() {
      Drawable drawable = mContext.getDrawable(mResourceId);
      drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
      return drawable;
 }
 }
The class can be used like this:
 SpannableString string = new SpannableString("Text with a drawable span");
 string.setSpan(new MyDynamicDrawableSpan(context, R.mipmap.ic_launcher), 12, 20, Spanned
 .SPAN_EXCLUSIVE_EXCLUSIVE);
Replacing text with a drawable.

Summary

Constants

int ALIGN_BASELINE

A constant indicating that the bottom of this span should be aligned with the baseline of the surrounding text.

int ALIGN_BOTTOM

A constant indicating that the bottom of this span should be aligned with the bottom of the surrounding text, i.e., at the same level as the lowest descender in the text.

int ALIGN_CENTER

A constant indicating that this span should be vertically centered between the top and the lowest descender.

Fields

protected final int mVerticalAlignment

Public constructors

DynamicDrawableSpan()

Creates a DynamicDrawableSpan.

Protected constructors

DynamicDrawableSpan(int verticalAlignment)

Creates a DynamicDrawableSpan based on a vertical alignment.\

Public methods

void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)

Draws the span into the canvas.

abstract Drawable getDrawable()

Your subclass must implement this method to provide the bitmap to be drawn.

int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm)

Returns the width of the span.

int getVerticalAlignment()

Returns the vertical alignment of this span, one of ALIGN_BOTTOM, ALIGN_BASELINE or ALIGN_CENTER.

String toString()

Returns a string representation of the object.

Inherited methods

Constants

ALIGN_BASELINE

Added in API level 3
public static final int ALIGN_BASELINE

A constant indicating that the bottom of this span should be aligned with the baseline of the surrounding text.

Constant Value: 1 (0x00000001)

ALIGN_BOTTOM

Added in API level 3
public static final int ALIGN_BOTTOM

A constant indicating that the bottom of this span should be aligned with the bottom of the surrounding text, i.e., at the same level as the lowest descender in the text.

Constant Value: 0 (0x00000000)

ALIGN_CENTER

Added in API level 29
public static final int ALIGN_CENTER

A constant indicating that this span should be vertically centered between the top and the lowest descender.

Constant Value: 2 (0x00000002)

Fields

mVerticalAlignment

Added in API level 3
protected final int mVerticalAlignment

Public constructors

DynamicDrawableSpan

Added in API level 1
public DynamicDrawableSpan ()

Creates a DynamicDrawableSpan. The default vertical alignment is ALIGN_BOTTOM

Protected constructors

DynamicDrawableSpan

Added in API level 1
protected DynamicDrawableSpan (int verticalAlignment)

Creates a DynamicDrawableSpan based on a vertical alignment.\

Parameters
verticalAlignment int: one of ALIGN_BOTTOM, ALIGN_BASELINE or ALIGN_CENTER Value is ALIGN_BOTTOM, ALIGN_BASELINE, or ALIGN_CENTER

Public methods

draw

Added in API level 1
public void draw (Canvas canvas, 
                CharSequence text, 
                int start, 
                int end, 
                float x, 
                int top, 
                int y, 
                int bottom, 
                Paint paint)

Draws the span into the canvas.

Parameters
canvas Canvas: This value cannot be null.

text CharSequence: Current text.

start int: Value is 0 or greater

end int: Value is 0 or greater

x float: Edge of the replacement closest to the leading margin.

top int: Top of the line.

y int: Baseline.

bottom int: Bottom of the line.

paint Paint: This value cannot be null.

getDrawable

Added in API level 1
public abstract Drawable getDrawable ()

Your subclass must implement this method to provide the bitmap to be drawn. The dimensions of the bitmap must be the same from each call to the next.

Returns
Drawable

getSize

Added in API level 1
public int getSize (Paint paint, 
                CharSequence text, 
                int start, 
                int end, 
                Paint.FontMetricsInt fm)

Returns the width of the span. Extending classes can set the height of the span by updating attributes of Paint.FontMetricsInt. If the span covers the whole text, and the height is not set, draw(android.graphics.Canvas, java.lang.CharSequence, int, int, float, int, int, int, android.graphics.Paint) will not be called for the span.

Parameters
paint Paint: This value cannot be null.

text CharSequence: Current text.

start int: Value is 0 or greater

end int: Value is 0 or greater

fm Paint.FontMetricsInt: This value may be null.

Returns
int Width of the span.

getVerticalAlignment

Added in API level 3
public int getVerticalAlignment ()

Returns the vertical alignment of this span, one of ALIGN_BOTTOM, ALIGN_BASELINE or ALIGN_CENTER.

Returns
int Value is ALIGN_BOTTOM, ALIGN_BASELINE, or ALIGN_CENTER

toString

Added in API level 1
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.