• 欢迎浏览“String me = Creater\忠实的资深Linux玩家;”,请文明浏览,理性发言,有侵犯你的权益请邮件我(creater@vip.qq.com).
  • 把任何的失败都当作一次尝试,不要自卑;把所有的成功都想成是一种幸运,不要自傲。
  •    3年前 (2015-07-10)  Android |   抢沙发  14 
    文章评分 0 次,平均分 0.0

    展示一个可以显示网页元素(包括图片)的封装TextView.先来看看效果图吧
    将TextView封装成可以显示html元素的RichText
    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

     

    除特别注明外,本站所有文章均为String me = "Creater\忠实的资深Linux玩家";原创,转载请注明出处来自http://unix8.net/home.php/4104.html

    关于

    发表评论

    暂无评论

    切换注册

    登录

    忘记密码 ?

    切换登录

    注册

    扫一扫二维码分享