Ho bisogno di sostituire frasi di testo in a con immagini e quindi aggiungerlo a un TextView. Per i Drawable regolari questo non è un problema, ma quando Drawable è un AnimationDrawable non so dove e quando chiamare .start();
.Carica dinamicamente un'AnimationDrawable in una TextView
questo è come mi aggiungere testo al TextView:
textview.append(Html.fromHtml(textWithHtmlImgTags, imagegetter, null));
I tag immagine nel textWithHtmlImgTags vengono sostituiti usando 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;
}
};
vengono aggiunti miei AnimationDrawables, ma non sono animati (sono bloccati sul fotogramma 1).
Nella documentazione si dice:
E 'importante notare che il metodo start() ha invitato l'AnimationDrawable non può essere chiamato durante il metodo onCreate() della vostra attività, perché l'AnimationDrawable non è ancora pienamente attaccato alla finestra. Se vuoi riprodurre immediatamente l'animazione, senza richiedere l'interazione, puoi chiamarla dal metodo onWindowFocusChanged() nella tua attività, che verrà chiamata quando Android mette a fuoco la tua finestra.
Poiché le immagini vengono aggiunte dinamicamente, non penso che abbia nulla a che fare con onCreate(). Quindi suppongo che chiamo il mio .start()
quando il mio drawable is not yet fully attached to the window
, ma dove/quando/come dovrei chiamare?
Grazie in anticipo!
dopo aver impostato i drawbales chiama 'textView.bringToFront()' e quindi chiama '.start()' per animare .. – MKJParekh