Android富文本解析器,支持網絡圖片,圖片和連接點擊事件

html-text

html-text 是 android.text.Html 的一個擴展,能夠加載 HTML 並將其轉換成 Spannable 顯示在 TextView 上,支持網絡圖片,圖片加載器無綁定,支持圖片和連接點擊事件,擴展了更多標籤。html

該庫體積微小,僅有8個類,不須要外部依賴。android

Screenshot

Supported HTML tags

Tags supported by android.text.Html

  • <p>
  • <div>
  • <br>
  • <b>
  • <i>
  • <strong>
  • <em>
  • <u>
  • <tt>
  • <dfn>
  • <sub>
  • <sup>
  • <blockquote>
  • <cite>
  • <big>
  • <small>
  • <font color="..." face="...">
  • <h1>, <h2>, <h3>, <h4>, <h5>, <h6>
  • <a href="...">
  • < img src="...">

Extended support by html-text

  • <ul>
  • <ol>
  • <li>
  • <code>
  • <center>
  • <strike>
  • <div>[HTML contains two newline, there is one]
  • <font size="..." color="...">[extend support size]
  • < img src="..." width="..." height="...">[extend support width, height]

Usage

Gradle

Step 1. Add the JitPack repository to your build filegit

Add it in your root build.gradle at the end of repositories:github

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
複製代碼

Step 2. Add the dependencyexpress

dependencies {
    compile 'com.github.wangchenyan:html-text:1.0'
}
複製代碼

Sample

TextView textView = (TextView) findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String sample = "<h2>Hello wold</h2>"
                 + "<font size=\"5\" color=\"#FF0000\">Font size</font>"
                 + "<img src=\"http://www.sample.com\"/>";
HtmlText.from(sample)
    .setImageLoader(new HtmlImageLoader() {
        @Override
        public void loadImage(String url, final Callback callback) {
            // Glide sample, you can also use other image loader
            Glide.with(context)
                 .load(url)
                 .asBitmap()
                 .into(new SimpleTarget<Bitmap>() {
                     @Override
                     public void onResourceReady(Bitmap resource,
                                                 GlideAnimation<? super Bitmap> glideAnimation) {
                         callback.onLoadComplete(resource);
                     }

                     @Override
                     public void onLoadFailed(Exception e, Drawable errorDrawable) {
                         callback.onLoadFailed();
                     }
                 });
        }

        @Override
        public Drawable getDefaultDrawable() {
            return ContextCompat.getDrawable(context, R.drawable.image_placeholder_loading);
        }

        @Override
        public Drawable getErrorDrawable() {
            return ContextCompat.getDrawable(context, R.drawable.image_placeholder_fail);
        }

        @Override
        public int getMaxWidth() {
            return getTextWidth();
        }

        @Override
        public boolean fitWidth() {
            return false;
        }
    })
    .setOnTagClickListener(new OnTagClickListener() {
        @Override
        public void onImageClick(Context context, List<String> imageUrlList, int position) {
            // image click
        }

        @Override
        public void onLinkClick(Context context, String url) {
            // link click
        }
    })
    .into(textView);
複製代碼

Thanks

License

Copyright 2017 wangchenyan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
複製代碼

遷移自個人簡書 2017.06.06apache

相關文章
相關標籤/搜索