How TextSize and LineSpace affects ImageSpan

守給你的承諾、 提交于 2019-12-11 06:25:38

问题


I want to measure ImageSpan with StaticLayout. to do this I use a simple code like below:

SpannableStringBuilder content = new SpannableStringBuilder();
Spannable imageSpannable = getImageSpannable();
content.append(imageSpannable);

StaticLayout layout = new StaticLayout(content, txt.getPaint(), txt.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1, lineSpace, false);

txt is a TextView and lineSpace just a variable indicating txt line space.

And getImageSpannable:

private Spannable getImageSpannable() {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.some_image);

    Drawable drawable = new BitmapDrawable(getResources(), bitmap);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());

    Spannable imageSpannable = new SpannableString("\uFFFC");
    ImageSpan imgSpan = new ImageSpan(drawable, DynamicDrawableSpan.ALIGN_BOTTOM);

    imageSpannable.setSpan(imgSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return imageSpannable;
}

When I add ImageSpan to SpannableStringBuilder(and nothing else) it seems that TextSize doesn't affect on ImageSpan. I mean when I call layout.getLineBottom(0) it always returns same value no matter what TextSize is. But line space affects.

But when I add "\n" and another String ( like "sample") after ImageSpan to SpannableStringBuilder, TextSize is also affects to ImageSpan height. In this situation ImageSpan have top margin equal to: (lineSpace + TextSize/2).

Can someone explain the exact behavior of ImageSpan, because I have to measure ImageSpan individually and concat to result.

How I should measure ImageSpan so that height of ImageSpan doesn't change after combining to other strings or even other ImageSpans?

来源:https://stackoverflow.com/questions/41671028/how-textsize-and-linespace-affects-imagespan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!