仿淘宝商品详情:TextView加载html内容

2,317 阅读1分钟

先看一下效果图:

1.首先xml中定义普通的TextView控件

        <TextView
            android:id="@+id/tv_content_html"
            android:layout_below="@+id/layout_info_more"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="50dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

2.重写图片加载接口 主要处理异步加载图片的操作

    /**
     * 重写图片加载接口
     *
     * @author Ruffian
     * @date 2016年1月15日
     *
     */
    class HtmlImageGetter implements Html.ImageGetter {

        /**
         * 获取图片
         */
        @Override
        public Drawable getDrawable(String source) {
            LevelListDrawable d = new LevelListDrawable();
            Drawable empty = getResources().getDrawable(
                    R.drawable.add_minus_bg);
            d.addLevel(0, 0, empty);
            d.setBounds(0, 0, DensityUtil.getDisplayWidth(),
                    empty.getIntrinsicHeight());
            new HtmlImageGetter.LoadImage().execute(source, d);
            return d;
        }

        /**
         * 异步下载图片类
         *
         * @author Ruffian
         * @date 2016年1月15日
         *
         */
        class LoadImage extends AsyncTask<Object, Void, Bitmap> {

            private LevelListDrawable mDrawable;

            @Override
            protected Bitmap doInBackground(Object... params) {
                String source = (String) params[0];
                mDrawable = (LevelListDrawable) params[1];
                try {
                    InputStream is = new URL(source).openStream();
                    return BitmapFactory.decodeStream(is);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            /**
             * 图片下载完成后执行
             */
            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    BitmapDrawable d = new BitmapDrawable(bitmap);
                    mDrawable.addLevel(1, 1, d);
                    /**
                     * 适配图片大小 <br/>
                     * 默认大小:bitmap.getWidth(), bitmap.getHeight()<br/>
                     * 适配屏幕:getDrawableAdapter
                     */
                    mDrawable = getDrawableAdapter(GoodDetailActivity.this, mDrawable,
                            bitmap.getWidth(), bitmap.getHeight());

                    // mDrawable.setBounds(0, 0, bitmap.getWidth(),
                    // bitmap.getHeight());

                    mDrawable.setLevel(1);

                    /**
                     * 图片下载完成之后重新赋值textView<br/>
                     * mtvActNewsContent:我项目中使用的textView
                     *
                     */
                    bindingView.tvContentHtml.invalidate();
                    CharSequence t = bindingView.tvContentHtml.getText();
                    bindingView.tvContentHtml.setText(t);

                }
            }

            /**
             * 加载网络图片,适配大小
             *
             * @param context
             * @param drawable
             * @param oldWidth
             * @param oldHeight
             * @return
             * @author Ruffian
             * @date 2016年1月15日
             */
            public LevelListDrawable getDrawableAdapter(Context context,
                                                        LevelListDrawable drawable, int oldWidth, int oldHeight) {
                LevelListDrawable newDrawable = drawable;
                long newHeight = 0;// 未知数
                int newWidth = DensityUtil.getDisplayWidth();// 默认屏幕宽
                newHeight = (newWidth * oldHeight) / oldWidth;
                // LogUtils.w("oldWidth:" + oldWidth + "oldHeight:" +
                // oldHeight);
                // LogUtils.w("newHeight:" + newHeight + "newWidth:" +
                // newWidth);
                newDrawable.setBounds(0, 0, newWidth, (int) newHeight);
                return newDrawable;
            }
        }

    }

3.TextView调用HtmlImageGetter加载html文本

                //默认图片,无图片或没加载完显示此图片
                //调用
                HtmlImageGetter htmlImageGetter = new HtmlImageGetter();
                Spanned spanned = Html.fromHtml(detail.getContent(), htmlImageGetter, null);
                bindingView.tvContentHtml.setText(spanned);
                bindingView.tvContentHtml.setMovementMethod(ScrollingMovementMethod.getInstance());// 设置可滚动
//                bindingView.tvContentHtml.setMovementMethod(LinkMovementMethod.getInstance());//设置超链接可以打开网页
//                bindingView.tvContentHtml.setText();

4.加载html文本内容如下:

"<img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/0589f5b87f126b1c4675e826274f691b.jpg\" alt=\"\" /><img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/60a5e3d9fd4740eb129ea0e538b0a7ee.jpg\" alt=\"\" /><img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/bc066e198db8b90f791125b532c7a8b2.jpg\" alt=\"\" /><img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/8a98671539ad84db971f127c79d69e15.jpg\" alt=\"\" /><img src=\"http://fudouzhongkang.oss-cn-shenzhen.aliyuncs.com/uploads/20200326/db7565dfa99525b8ea9d20d1daadc49f.jpg\" alt=\"\" />"