HtmlText
image
HtmlText 是 android.text.Html 的一个扩展,可以加载 HTML 并将其转换成 Spannable 显示在 TextView 上,支持网络图片,图片加载器无绑定,支持图片和链接点击事件,扩展了更多标签。
该库体积微小,仅有8个类,不需要外部依赖。
Screenshot
[图片上传失败...(image-e3ca7b-1522079821669)]
Supported HTML tags
Tags supported by android.text.Html
,
,
,
,
,
< img src="...">
Extended support by HtmlText
[HTML contains two newline, there is one][extend support size]
< img src="..." width="..." height="...">[extend support width, height]
Usage
Gradle
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.wangchenyan:HtmlText:1.0'
}
Sample
TextView textView = (TextView) findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String sample = "
Hello wold
"+ "Font size"
+ "";
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() {
@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 imageUrlList, int position) {
// image click
}
@Override
public void onLinkClick(Context context, String url) {
// link click
}
})
.into(textView);
Thanks
作者:ChayWong
链接:https://www.jianshu.com/p/4d2d04b3889f
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。