Reduce ImageSpan height and width
Asked Answered
D

1

9

I'm setting an Image Drawable as a SpannableString to a TextView but the image comes out larger than the text making it look weird. I need to reduce the size of the imagespan such that it's the same height as the text:

Here's what I've tried:

drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

SpannableStringBuilder builder = new SpannableStringBuilder(holder.temptext.getText());
builder.setSpan(new ImageSpan(drawable), selectionCursor - ":)".length(), selectionCursor, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.temptext.setText(builder);
holder.temptext.setSelection(selectionCursor);

holder.caption.setText(builder);
Darwen answered 30/8, 2015 at 16:41 Comment(0)
K
16

You need to measure your TextView's height and use it instead of intrinsic bounds:

int lineHeight = holder.temptext.getLineHeight();

drawable.setBounds(0, 0, lineHeight, lineHeight);
Krissykrista answered 7/9, 2015 at 23:27 Comment(2)
this doesn't maintain the image's aspect ratioPotts
You can calculate the new width like this val width = lineHeight * drawable.intrinsicWidth / drawable.intrinsicHeight.Butts

© 2022 - 2024 — McMap. All rights reserved.