GlideImageSpan is a ReplacementSpan that uses the Glide library to load and display images (including GIFs) directly within a TextView. It allows for advanced styling such as vertical alignment, custom sizing, margins, padding, and even overlaying text on top of the image.
Prerequisites
You must include the Glide dependency in your project to use this class.
Key Features
- Vertical Alignment: Choose between
Align.CENTER (default), Align.BASELINE, or Align.BOTTOM. - Sizing: Set custom widths and heights. Use
-1 to match the text width/height or 0 for the image's original size. - Text Overlay: You can display text on top of the image by enabling
setTextVisibility and configuring textGravity and textOffset. - GIF Support: Supports playing GIFs with configurable loop counts.
- Glide Integration: Pass custom
RequestOptions to control placeholders, error drawables, and transformations (e.g., centerCrop).
// Example usage (Conceptual)
val span = GlideImageSpan(textView, "https://example.com/image.jpg")
.setAlign(GlideImageSpan.Align.CENTER)
.setDrawableSize(200, 200)
.setMarginHorizontal(10)
.setTextVisibility(true)
.setTextGravity(Gravity.CENTER)
textView.text = "Some text " + span + " more text"
// To use custom Glide options
val options = RequestOptions().placeholder(R.drawable.placeholder).centerCrop()
span.setRequestOption(options)