Package icyllis.modernui.text.style

# Class DynamicDrawableSpan

java.lang.Object
icyllis.modernui.text.style.CharacterStyle
icyllis.modernui.text.style.MetricAffectingSpan
icyllis.modernui.text.style.ReplacementSpan
icyllis.modernui.text.style.DynamicDrawableSpan

All Implemented Interfaces:
  `UpdateAppearance`, `UpdateLayout`

Direct Known Subclasses:
  `ImageSpan`

public abstract class DynamicDrawableSpan extends ReplacementSpan
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.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static @interface`
  `DynamicDrawableSpan.AlignmentType`
  Defines acceptable alignment types.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `ALIGN_BASELINE`
  A constant indicating that the bottom of this span should be aligned with the baseline of the surrounding text.
  `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.
  `static final int`
  `ALIGN_CENTER`
  A constant indicating that this span should be vertically centered between the top and the lowest descender.
  `protected final int`
  `mVerticalAlignment`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  ``
  `DynamicDrawableSpan()`
  Creates a `DynamicDrawableSpan`.
  `protected`
  `DynamicDrawableSpan(int verticalAlignment)`
  Creates a `DynamicDrawableSpan` based on a vertical alignment.\

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, TextPaint 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(TextPaint paint, CharSequence text, int start, int end, 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`.

  ### Methods inherited from class icyllis.modernui.text.style.ReplacementSpan

  `updateMeasureState`

  ### Methods inherited from class icyllis.modernui.text.style.MetricAffectingSpan

  `getUnderlying, updateDrawState`

  ### Methods inherited from class icyllis.modernui.text.style.CharacterStyle

  `wrap`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### ALIGN_BOTTOM

    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.
    See Also:
      - Constant Field Values

  - ### ALIGN_BASELINE

    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.
    See Also:
      - Constant Field Values

  - ### ALIGN_CENTER

    public static final int ALIGN_CENTER
    A constant indicating that this span should be vertically centered between the top and the lowest descender.
    See Also:
      - Constant Field Values

  - ### mVerticalAlignment

    protected final int mVerticalAlignment

- ## Constructor Details

  - ### DynamicDrawableSpan

    public DynamicDrawableSpan()
    Creates a `DynamicDrawableSpan`. The default vertical alignment is `ALIGN_BOTTOM`

  - ### DynamicDrawableSpan

    protected DynamicDrawableSpan(int verticalAlignment)
    Creates a `DynamicDrawableSpan` based on a vertical alignment.\
    Parameters:
      `verticalAlignment` - one of `ALIGN_BOTTOM`, `ALIGN_BASELINE` or `ALIGN_CENTER`

- ## Method Details

  - ### getVerticalAlignment

    public int getVerticalAlignment()
    Returns the vertical alignment of this span, one of `ALIGN_BOTTOM`, `ALIGN_BASELINE` or `ALIGN_CENTER`.

  - ### getDrawable

    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.

  - ### getSize

    public int getSize(@Nonnull TextPaint paint, CharSequence text, int start, int end, @Nullable FontMetricsInt fm)
    Description copied from class: `ReplacementSpan`
    Returns the width of the span. Extending classes can set the height of the span by updating attributes of `FontMetricsInt`. If the span covers the whole text, and the height is not set, `ReplacementSpan.draw(Canvas, CharSequence, int, int, float, int, int, int, TextPaint)` will not be called for the span.
    Specified by:
      `getSize` in class `ReplacementSpan`
    Parameters:
      `paint` - Paint instance.
      `text` - Current text.
      `start` - Start character index for span.
      `end` - End character index for span.
      `fm` - Font metrics, can be null.
    Returns:
      Width of the span.

  - ### draw

    public void draw(@Nonnull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @Nonnull TextPaint paint)
    Description copied from class: `ReplacementSpan`
    Draws the span into the canvas.
    Specified by:
      `draw` in class `ReplacementSpan`
    Parameters:
      `canvas` - Canvas into which the span should be rendered.
      `text` - Current text.
      `start` - Start character index for span.
      `end` - End character index for span.
      `x` - Edge of the replacement closest to the leading margin.
      `top` - Top of the line.
      `y` - Baseline.
      `bottom` - Bottom of the line.
      `paint` - Paint instance.