我需要用图像替换文本中的短语,然后将其附加到TextView。对于普通Drawables来说这没有问题,但是当Drawable是一个AnimationDrawable时,我不知道何时何地调用.start();
。动态加载TextView中的AnimationDrawable
这是我如何添加文字的TextView:
textview.append(Html.fromHtml(textWithHtmlImgTags, imagegetter, null));
在textWithHtmlImgTags图片代码使用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;
}
};
我AnimationDrawables添加,但它们不是动画(他们卡在第1帧)。
在本文档中,它说:
需要注意的是不能将活动的onCreate()方法中调用呼吁AnimationDrawable的start()方法是很重要的,因为AnimationDrawable尚未完全附在窗户上。如果您想要立即播放动画而不需要交互,那么您可能需要从Activity中的onWindowFocusChanged()方法调用该动画,这会在Android将窗口放到焦点上时调用。
由于图像是动态添加的,我不认为它与onCreate()有任何关系。 所以我想我打电话给我.start()
当我的可绘制的is not yet fully attached to the window
,但何时/何时/如何应该我称之为?
在此先感谢!
设置drawbales之后调用'textView.bringToFront()',然后调用'.start()'使其生成动画.. – MKJParekh 2012-04-16 12:09:46