androidandroid-drawablespannablestringimagespan

Modify Drawable inside SpannableStringBuilder and DynamicLayout


I'm trying to hide a smiley in a SpannableStringBuilder (which is used in a DynamicLayout). At first I tried

ImageSpan[] spans = spannable.getSpans(0, length, ImageSpan.class);
spans[0].getDrawable().setAlpha(0);

It didn't work. I also tried

ImageSpan[] spans = spannable.getSpans(0, length, ImageSpan.class);
spans[0].getDrawable().setVisible(false, true);

Didn't work either.

The next code did work, but it doesn't preserve the size of the drawable, so the text around the smiley starts to "jump", that is not correct.

ImageSpan[] spans = spannable.getSpans(0, length, ImageSpan.class);
spannable.removeSpan(spans[0]);

Potentially, I could redraw the drawable, but it's an expensive operation. Why doesn't the drawable update? What am I missing?

Thanks in advance.


Solution

  • Okay, in my base drawable class I implemented the setAlpha method in which I changed the paint object. After that I revalidated the view in which the drawable was. After that the draw method in my drawable class gets executed and the alpha value changes