将TextView封装成可以显示html元素的RichText

2015年7月10日 由 Creater 留言 »

展示一个可以显示网页元素(包括图片)的封装TextView.先来看看效果图吧
Screenshot_2015-07-10-21-46-34
RichTextView.java

package com.example.richtext;

import android.content.Context;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.TextView;


import com.example.richtext.AsyncImageGetter;


import java.util.ArrayList;

public class RichTextView extends TextView {

    public RichTextView(Context context) {
        super(context);
    }

    public RichTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RichTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setRichText(String text) {

        Spanned spanned = Html.fromHtml(text, new AsyncImageGetter(getContext(), this), null);
        SpannableStringBuilder htmlSpannable;
        if (spanned instanceof SpannableStringBuilder) {
            htmlSpannable = (SpannableStringBuilder) spanned;
        } else {
            htmlSpannable = new SpannableStringBuilder(spanned);
        }

        {
            ImageSpan[] spans = htmlSpannable.getSpans(0, htmlSpannable.length(), ImageSpan.class);
            final ArrayList<String> imageUrls = new ArrayList<String>();
            final ArrayList<String> imagePositions = new ArrayList<String>();
            for (ImageSpan currentSpan : spans) {
                final String imageUrl = currentSpan.getSource();
                final int start = htmlSpannable.getSpanStart(currentSpan);
                final int end = htmlSpannable.getSpanEnd(currentSpan);
                imagePositions.add(start + "," + end);
                imageUrls.add(imageUrl);
            }
        }

        super.setText(spanned);
        setMovementMethod(LinkMovementMethod.getInstance());
    }
}

图像加载器,使用了universalimageloader。

package com.example.richtext;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.example.richtext.R;

public class AsyncImageGetter implements Html.ImageGetter {

    private Context mContext;
    //
    private TextView mContainer;
    private Drawable mDefaultDrawable;
    private int mMaxWidth;

    public AsyncImageGetter(Context context, TextView container){
        mContext = context;
        mContainer = container;
        mMaxWidth = ScreenUtils.getDisplayWidth(mContext) -  ScreenUtils.dp(mContext, 100);
        //默认图片
        mDefaultDrawable = context.getResources().getDrawable(R.drawable.ic_launcher);
    }

    @Override
    public Drawable getDrawable(String source) {
        final URLDrawable urlDrawable = new URLDrawable();
        ImageLoader.getInstance().loadImage(source, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                if (bitmap != null) {
                    int width;
                    int height;
                    if (bitmap.getWidth() > mMaxWidth) {
                        width = mMaxWidth;
                        height = mMaxWidth * bitmap.getHeight() / bitmap.getWidth();
                    } else {
                        width = bitmap.getWidth();
                        height = bitmap.getHeight();
                    }
                    Drawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);
                    drawable.setBounds(0, 0, width, height);
                    urlDrawable.setBounds(0, 0, width, height);
                    urlDrawable.mDrawable = drawable;
                    //reset text to invalidate.
                    mContainer.setText(mContainer.getText());
                }
            }
        });
        return urlDrawable;
    }

    public class URLDrawable extends BitmapDrawable{

        protected Drawable mDrawable;

        @Override
        public void draw(Canvas canvas) {
            if(mDrawable != null){
                mDrawable.draw(canvas);
            }else{
                mDefaultDrawable.draw(canvas);
            }
        }
    }
}

附上eclipse demoworkspace

广告位

发表评论

你必须 登陆 方可发表评论.