How do I measure the height of a SpannableString?

I have a TextView in which I am showing a SpannableString with different Span sizes. I need to know the exact height of the last word in the SpannableString, but the method I am using is wrong. What am I doing wrong?

Here is the test code I am using

    SpannableStringBuilder spannableString = new SpannableStringBuilder("Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text");
    spannableString.setSpan(new AbsoluteSizeSpan(10, true), 0, spannableString.length() / 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new AbsoluteSizeSpan(15, true), spannableString.length()/3, 2 * spannableString.length()/3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new AbsoluteSizeSpan(20, true), 2 * spannableString.length() / 3, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    surfaceView.setTranslationY(measureSpannableHeight());

private int measureSpannableHeight() {
    StaticLayout staticLayout = new StaticLayout(spannableString, new TextPaint(), 480, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
    return staticLayout.getHeight();
}

      

(surface conversion is considered only to illustrate the height measurement result)

+3


source to share





All Articles