How TextSize and LineSpace affects ImageSpan

2019-07-28 20:11发布

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?

0条回答
登录 后发表回答