ImageSpan


public class ImageSpan
extends DynamicDrawableSpan

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


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. The drawable can be constructed from varied sources:

The default value for the vertical alignment is DynamicDrawableSpan.ALIGN_BOTTOM

For example, an ImagedSpan can be used like this:

 SpannableString string = new SpannableString("Bottom: span.\nBaseline: span.");
 // using the default alignment: ALIGN_BOTTOM
 string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 7, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher, DynamicDrawableSpan.ALIGN_BASELINE),
 22, 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
Text with ImageSpans aligned bottom and baseline.

Summary

Inherited 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.

Inherited fields

protected final int mVerticalAlignment

Public constructors

ImageSpan(Context context, Bitmap bitmap)

Constructs an ImageSpan from a Context and a Bitmap with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM

ImageSpan(Context context, Bitmap bitmap, int verticalAlignment)

Constructs an ImageSpan from a Context, a Bitmap and a vertical alignment.

ImageSpan(Context context, Uri uri)

Constructs an ImageSpan from a Context and a Uri with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM.

ImageSpan(Context context, Uri uri, int verticalAlignment)

Constructs an ImageSpan from a Context, a Uri and a vertical alignment.

ImageSpan(Context context, int resourceId)

Constructs an ImageSpan from a Context and a resource id with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM

ImageSpan(Context context, int resourceId, int verticalAlignment)

Constructs an ImageSpan from a Context, a resource id and a vertical alignment.

ImageSpan(Bitmap b)

This constructor is deprecated. Use ImageSpan(android.content.Context, android.graphics.Bitmap) instead.

ImageSpan(Bitmap b, int verticalAlignment)

This constructor is deprecated. Use ImageSpan(android.content.Context, android.graphics.Bitmap, int) instead.

ImageSpan(Drawable drawable)

Constructs an ImageSpan from a drawable with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM.

ImageSpan(Drawable drawable, int verticalAlignment)

Constructs an ImageSpan from a drawable and a vertical alignment.

ImageSpan(Drawable drawable, String source)

Constructs an ImageSpan from a drawable and a source with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM

ImageSpan(Drawable drawable, String source, int verticalAlignment)

Constructs an ImageSpan from a drawable, a source and a vertical alignment.

Public methods

Drawable getDrawable()

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

String getSource()

Returns the source string that was saved during construction.

String toString()

Returns a string representation of the object.

Inherited 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.

abstract 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.

CharSequence getContentDescription()

Gets a brief description of this ReplacementSpan for use in accessibility support.

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

Returns the width of the span.

void setContentDescription(CharSequence contentDescription)

Sets the specific content description into ReplacementSpan.

void updateDrawState(TextPaint ds)

This method does nothing, since ReplacementSpans are drawn explicitly instead of affecting Paint properties.

void updateMeasureState(TextPaint p)

This method does nothing, since ReplacementSpans are measured explicitly instead of affecting Paint properties.

MetricAffectingSpan getUnderlying()

Returns "this" for most MetricAffectingSpans, but for MetricAffectingSpans that were generated by CharacterStyle.wrap(CharacterStyle), returns the underlying MetricAffectingSpan.

abstract void updateMeasureState(TextPaint textPaint)

Classes that extend MetricAffectingSpan implement this method to update the text formatting in a way that can change the width or height of characters.

CharacterStyle getUnderlying()

Returns "this" for most CharacterStyles, but for CharacterStyles that were generated by wrap(CharacterStyle), returns the underlying CharacterStyle.

abstract void updateDrawState(TextPaint tp)
static CharacterStyle wrap(CharacterStyle cs)

A given CharacterStyle can only applied to a single region of a given Spanned.

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

ImageSpan

Added in API level 4
public ImageSpan (Context context, 
                Bitmap bitmap)

Constructs an ImageSpan from a Context and a Bitmap with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources This value cannot be null.

bitmap Bitmap: bitmap to be rendered This value cannot be null.

ImageSpan

Added in API level 4
public ImageSpan (Context context, 
                Bitmap bitmap, 
                int verticalAlignment)

Constructs an ImageSpan from a Context, a Bitmap and a vertical alignment.

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources This value cannot be null.

bitmap Bitmap: bitmap to be rendered This value cannot be null.

verticalAlignment int: one of DynamicDrawableSpan.ALIGN_BOTTOM or DynamicDrawableSpan.ALIGN_BASELINE

ImageSpan

Added in API level 1
public ImageSpan (Context context, 
                Uri uri)

Constructs an ImageSpan from a Context and a Uri with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM. The Uri source can be retrieved via getSource()

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources This value cannot be null.

uri Uri: Uri used to construct the drawable that will be rendered This value cannot be null.

ImageSpan

Added in API level 3
public ImageSpan (Context context, 
                Uri uri, 
                int verticalAlignment)

Constructs an ImageSpan from a Context, a Uri and a vertical alignment. The Uri source can be retrieved via getSource()

Parameters
context Context: context used to create a drawable from based on the display metrics of the resources This value cannot be null.

uri Uri: Uri used to construct the drawable that will be rendered. This value cannot be null.

verticalAlignment int: one of DynamicDrawableSpan.ALIGN_BOTTOM or DynamicDrawableSpan.ALIGN_BASELINE

ImageSpan

Added in API level 1
public ImageSpan (Context context, 
                int resourceId)

Constructs an ImageSpan from a Context and a resource id with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM

Parameters
context Context: context used to retrieve the drawable from resources This value cannot be null.

resourceId int: drawable resource id based on which the drawable is retrieved

ImageSpan

Added in API level 3
public ImageSpan (Context context, 
                int resourceId, 
                int verticalAlignment)

Constructs an ImageSpan from a Context, a resource id and a vertical alignment.

Parameters
context Context: context used to retrieve the drawable from resources This value cannot be null.

resourceId int: drawable resource id based on which the drawable is retrieved.

verticalAlignment int: one of DynamicDrawableSpan.ALIGN_BOTTOM or DynamicDrawableSpan.ALIGN_BASELINE

ImageSpan

Added in API level 1
public ImageSpan (Bitmap b)

This constructor is deprecated.
Use ImageSpan(android.content.Context, android.graphics.Bitmap) instead.

Parameters
b Bitmap: This value cannot be null.

ImageSpan

Added in API level 3
public ImageSpan (Bitmap b, 
                int verticalAlignment)

This constructor is deprecated.
Use ImageSpan(android.content.Context, android.graphics.Bitmap, int) instead.

Parameters
b Bitmap: This value cannot be null.

verticalAlignment int

ImageSpan

Added in API level 1
public ImageSpan (Drawable drawable)

Constructs an ImageSpan from a drawable with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM.

Parameters
drawable Drawable: drawable to be rendered This value cannot be null.

ImageSpan

Added in API level 3
public ImageSpan (Drawable drawable, 
                int verticalAlignment)

Constructs an ImageSpan from a drawable and a vertical alignment.

Parameters
drawable Drawable: drawable to be rendered This value cannot be null.

verticalAlignment int: one of DynamicDrawableSpan.ALIGN_BOTTOM or DynamicDrawableSpan.ALIGN_BASELINE

ImageSpan

Added in API level 1
public ImageSpan (Drawable drawable, 
                String source)

Constructs an ImageSpan from a drawable and a source with the default alignment DynamicDrawableSpan.ALIGN_BOTTOM

Parameters
drawable Drawable: drawable to be rendered This value cannot be null.

source String: drawable's Uri source This value cannot be null.

ImageSpan

Added in API level 3
public ImageSpan (Drawable drawable, 
                String source, 
                int verticalAlignment)

Constructs an ImageSpan from a drawable, a source and a vertical alignment.

Parameters
drawable Drawable: drawable to be rendered This value cannot be null.

source String: drawable's uri source This value cannot be null.

verticalAlignment int: one of DynamicDrawableSpan.ALIGN_BOTTOM or DynamicDrawableSpan.ALIGN_BASELINE

Public methods

getDrawable

Added in API level 1
public 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

getSource

Added in API level 1
public String getSource ()

Returns the source string that was saved during construction.

Returns
String the source string that was saved during construction This value may be null.

toString

Added in API level 1
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.