Android TextView 富文本之 ImageSpan

本文介绍了Android中TextView的富文本特性ImageSpan,包括其来源于ReplacementSpan,详解getSize和draw方法,以及如何处理居中对齐、省略号丢失等问题。通过实例展示了不同对齐方式的效果,并提供了问题解决方案。
摘要由CSDN通过智能技术生成

前言

我们现在在 Android App 中几乎天天都能见到 ImageSpan,比如 App 自定义的 emoji 表情和文本中带的一些小图标等。


你可能要问了既然都有 emoji 了,为啥还要自定义呢,输入法里的 emoji 和 ImageSpan 的有啥不一样呢?。要做这玩意儿肯定还是产品提的需求啦,想自己原创一些比较 的表情吧。现在好多输入法都自带的 emoji 只是一串字符,TextView 在渲染的时候会用系统自带的表情把它们画出来,而ImageSpan的话就需要自己手动画对应的图片。
我在使用ImageSpan的过程中也遇到了一些坑,在这里就做下总结,分享出来,有什么不对的地方,也请各路大佬批评指正。

ImageSpan

1、ImageSpan 来自 ReplacementSpan


ReplacementSpan中空实现了updateMeasureStateupdateDrawState

/**
 * This method does nothing, since ReplacementSpans are measured
 * explicitly instead of affecting Paint properties.
 */
public void updateMeasureState(TextPaint p) {
    }

/**
 * This method does nothing, since ReplacementSpans are drawn
 * explicitly instead of affecting Paint properties.
 */
public void updateDrawState(TextPaint ds) {
    }

而新增了getSizedraw来进行图片位置的确定以及图片内容的绘制


/**
 * Returns the width of the span. Extending classes can set the height of the span by updating
 * attributes of {@link android.graphics.Paint.FontMetricsInt}. If the span covers the whole
 * text, and the height is not set,
 * {@link #draw(Canvas, CharSequence, int, int, float, int, int, int, Paint)} will not be
 * called for the span.
 *
 * @param paint Paint instance.
 * @param text Current text.
 * @param start Start character index for span.
 * @param end End character index for span.
 * @param fm Font metrics, can be null.
 * @return Width of the span.
 */
public abstract int getSize(@NonNull Paint paint, CharSequence text,
                    @IntRange(from = 0) int start, @IntRange(from = 0) int end,
                    @Nullable Paint.FontMetricsInt fm);

/**
 * Draws the span into the canvas.
 *
 * @param canvas Canvas into which the span should be rendered.
 * @param text Current text.
 * @param start Start character index for span.
 * @param end End character index for span.
 * @param x Edge of the replacement closest to the leading margin.
 * @param top Top of the line.
 * @param y Baseline.
 * @param bottom Bottom of the line.
 * @param paint Paint instance.
 */
public abstract void draw(@NonNull Canvas canvas, C
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值