我有ListView项目,每个项目有5个TextView,其中一个TextView从Html.fromHtml函数获取文本,我使用ImageGetter函数处理HTML中的img标签,图像下载异步,而图像下载我想显示AnimationDrawable(循环进度条)。
以下是我的ImageGetter实现:
holder.CommentText.setText(Html.fromHtml(commentObj.getComment(), new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable d = null;
if(UserSettings.LoadImages)
{
if(commentObj.getImages()==null)
{
commentObj.setImages(new HashMap<String, Drawable>());
}
String imageLink=source;
if(!imageLink.contains("http://"))
imageLink="http://rotter.net"+source;
if(commentObj.getImages().containsKey(imageLink))
{
d=commentObj.getImages().get(imageLink);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
}
else
{
AnimationDrawable animated=(AnimationDrawable)context.getResources().getDrawable(R.anim.image_loading);
animated.setBounds(0, 0, animated.getIntrinsicWidth(), animated.getIntrinsicHeight());
//animated.start();
commentObj.getImages().put(imageLink, animated);
DownloadImage downImg=new DownloadImage(commentObj, imageLink,v,adapter,Width,context.getResources());
holder.CommentText.onAttachedToWindow();
downImg.execute(new String[] { imageLink } );
return animated;
}
}
return d;
}
},null ));
经过一些阅读后,我创建了新的自定义TextView类:
public class NewTextView extends TextView {
private Handler mHandler=new Handler();
public NewTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public NewTextView(Context context,AttributeSet attributeSet) {
super(context,attributeSet);
// TODO Auto-generated constructor stub
}
@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);
}
}
}
}
}
@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);
}
}
}
NewTextView类负责启动和停止AnimationDrawable , 结果是一些AnimationDrawables正常运行(循环进度条)和一些AnimationDrawables根本不运行,它们只显示AnimationDrawable项目中的第一个Drawable。
可能是什么问题?为什么所有的AnimationDrawable都不能正常运行?
答案 0 :(得分:0)
你可以检查项目的方法&#34; onAttachedToWindow&#34;多次调用,每次都会导致无效。