0

ListView には項目があり、各項目には 5 つの TextView があり、この TextView の 1 つは 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);
        }
    }
 }

AnimationDrawable の開始と停止を担当する NewTextView クラス。その結果、いくつかの AnimationDrawable が正しく実行され (サイクル プログレス バー)、一部の AnimationDrawable がまったく実行されず、AnimationDrawable アイテムの最初の Drawable のみが表示されます。

何が問題になる可能性がありますか? すべての AnimationDrawables が適切に実行されないのはなぜですか?

4

1 に答える 1