I need to replace phrases of text in a with images and then append that to a TextView. For regular Drawables this is no problem, but when the Drawable is an AnimationDrawable I don't know where and when to call .start();
.
This is how I append text to the TextView:
textview.append(Html.fromHtml(textWithHtmlImgTags, imagegetter, null));
The image tags in the textWithHtmlImgTags are replaced using imagegetter:
new ImageGetter()
{
@Override
public Drawable getDrawable(String source) {
if(source.endsWith("_ani"))
{
Log.i("cmv", "This is an animated drawable.");
AnimationDrawable dra = (AnimationDrawable)res.getDrawable(sRes.get(source));
dra.setBounds(0, 0, dra.getIntrinsicWidth(), dra.getIntrinsicHeight());
dra.start(); // This doesn't work..
return dra;
}
Drawable dr = res.getDrawable(sRes.get(source));
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
return dr;
}
};
My AnimationDrawables are added, but they aren't animated (they're stuck on frame 1).
In the documentation it says:
It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.
Since the images are added dynamically I don't think it has anything to do with onCreate().
So I guess I call my .start()
when my drawable is not yet fully attached to the window
, but where/when/how should I call it?
Thanks in advance!
I came out with the solution. In your custom TextView:
(1) First, you have to decide the timing of the animation start and stop.
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
handleAnimationDrawable(true);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handleAnimationDrawable(false);
}
private void handleAnimationDrawable(boolean isPlay) {
CharSequence text = getText();
if (text instanceof Spanned) {
Spanned span = (Spanned) text;
ImageSpan[] spans = span.getSpans(0, span.length() - 1,
ImageSpan.class);
for (ImageSpan s : spans) {
Drawable d = s.getDrawable();
if (d instanceof AnimationDrawable) {
AnimationDrawable animationDrawable = (AnimationDrawable) d;
if (isPlay) {
animationDrawable.setCallback(this);
animationDrawable.start();
} else {
animationDrawable.stop();
animationDrawable.setCallback(null);
}
}
}
}
}
(2) And then implement your own Drawable.Callback to trigger the redraw.
@Override
public void invalidateDrawable(Drawable dr) {
invalidate();
}
@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
if (who != null && what != null) {
mHandler.postAtTime(what, when);
}
}
@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
if (who != null && what != null) {
mHandler.removeCallbacks(what);
}
}
I guess the important question is: when do you make the call ?
textview.append(Html.fromHtml(textWithHtmlImgTags...
If you are calling this after onCreate then you can call the animation.start() . If it is called in onCreate() method, I would:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With