android– 使用Spanable String对齐ImageSpan

android– 使用Spanable String对齐ImageSpan,第1张

概述我知道有很多相同类型的问题可供使用,我尝试了很多解决方案,但所有这些问题都达不到我的要求.我的问题是我必须在包含Spanable字符串和Imagespan的文本之间添加动态行间距,但是当我添加行间距时,文本和图像的对齐会变形.我已经尝试了Stackoverflow上几乎所有可用的解决方案,如thi

我知道有很多相同类型的问题可供使用,我尝试了很多解决方案,但所有这些问题都达不到我的要求.@H_502_1@

我的问题是我必须在包含Spanable字符串和Imagespan的文本之间添加动态行间距,但是当我添加行间距时,文本和图像的对齐会变形.@H_502_1@

我已经尝试了Stackoverflow上几乎所有可用的解决方案,如this,this& this但是一切都在静脉中.
我附上了截图
@H_502_1@

>添加动态行间距之前的屏幕截图@H_502_1@


 2.添加动态行间距后的屏幕截图@H_502_1@

@H_502_1@

任何帮助将受到高度赞赏.提前致谢!@H_502_1@

解决方法:@H_502_1@

在onDraw方法中使用“y”查找文本的基线,然后将drawable与文本视图的基线对齐@H_502_1@

@H_502_1@

public class VerticalimageSpan extends ImageSpan { public VerticalimageSpan(Drawable drawable) {        super(drawable);    }    /**     * update the text line height     */    @OverrIDe    public int getSize(Paint paint, CharSequence text, int start, int end,                       Paint.FontMetricsInt FontMetricsInt) {        Drawable drawable = getDrawable();        Rect rect = drawable.getBounds();        if (FontMetricsInt != null) {            Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();            int FontHeight = fmPaint.descent - fmPaint.ascent;            int drHeight = rect.bottom - rect.top;            int centerY = fmPaint.ascent + FontHeight / 2;            FontMetricsInt.ascent = centerY - drHeight / 2;            FontMetricsInt.top = FontMetricsInt.ascent;            FontMetricsInt.bottom = centerY + drHeight / 2;            FontMetricsInt.descent = FontMetricsInt.bottom;        }        return rect.right;    }    /**     * see detail message in androID.text.Textline     *     * @param canvas the canvas, can be null if not rendering     * @param text   the text to be draw     * @param start  the text start position     * @param end    the text end position     * @param x      the edge of the replacement closest to the leading margin     * @param top    the top of the line     * @param y      the baseline     * @param bottom the bottom of the line     * @param paint  the work paint     */    @OverrIDe    public voID draw(Canvas canvas, CharSequence text, int start, int end,                     float x, int top, int y, int bottom, Paint paint) {        Drawable drawable = getDrawable();        canvas.save();        Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();        int FontHeight = fmPaint.descent - fmPaint.ascent;        int centerY = y + fmPaint.descent - FontHeight / 2;        int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;        canvas.translate(x, transY);        drawable.draw(canvas);        canvas.restore();    }}
总结

以上是内存溢出为你收集整理的android – 使用Spanable String对齐ImageSpan全部内容,希望文章能够帮你解决android – 使用Spanable String对齐ImageSpan所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1118455.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存