先上图,比如微博的text数据:
解析的格式类似:
<img alt=[允悲] src=\"//h5.sinaimg.cn/m/emoticon/icon/default/d_yunbei-9aa3c436a4.png\" style=\"width:1em; height:1em;\" />
对textview的设置:
holder.tv_text.setText(Html.fromHtml(statusBean.getText(), new MyImageGetter(context, holder.tv_text), null));
其中MyImageGetter()
为:
采用Glide异步加载,因为表情是常用图,设置本地磁盘缓存,图片位置可以自己调整
/**
* Created by kusunoki on 2020/5/23 0023 14:30.
* 用于加载textView中表情图片
*/
public class MyImageGetter implements Html.ImageGetter {
private URLDrawable urlDrawable = null;
private TextView textView;
private Context context;
public MyImageGetter(Context context, TextView textView) {
this.textView = textView;
this.context = context;
}
@Override
public Drawable getDrawable(final String source) {
urlDrawable = new URLDrawable();
String url; // 因为渣浪的表情url有时候没有加http头,所以有这个判断
if (!source.contains("http")){
url = "https:" + source;
}else {
url = source;
}
Glide.with(context)
.asBitmap()
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
// 这部分是修改bitmap图片的大小,返回来的表情太小了
Matrix matrix = new Matrix();
matrix.postScale(1.5f,1.5f);
Bitmap newBitmap = Bitmap.createBitmap(resource,0,0,resource.getWidth(),resource.getHeight(),matrix,false);
urlDrawable.bitmap = newBitmap;
urlDrawable.setBounds(0, 0, newBitmap.getWidth(), newBitmap.getHeight());
textView.invalidate();
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
return urlDrawable;
}
public class URLDrawable extends BitmapDrawable {
public Bitmap bitmap;
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, getPaint());
}
}
}
}
解析超链接可参考:
//www.greatytc.com/p/e27532aa7cb2
自己写的微博html版@的超链接用户跳转:
/**
* Created by kusunoki on 2020/5/23 0023 21:08.
* 参考//www.greatytc.com/p/de71858168cd
* 和//www.greatytc.com/p/e27532aa7cb2
*/
public class MyHTML {
static private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,
final URLSpan urlSpan, Context context, TextView textView) {
int start = clickableHtmlBuilder.getSpanStart(urlSpan);
int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
String url = "https://m.weibo.cn/" + urlSpan.getURL();
RequestQueue queue = VolleySingleton.getinstance(context.getApplicationContext()).getRequestQueue();
StringRequest stringRequest = new StringRequest(
Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String p = "(?<=value\":\")\\d*?(?=\")";
Pattern pattern = Pattern.compile(p);
Matcher m = pattern.matcher(response);
if(m.find()){
String userID = m.group(0);
NavController navController = Navigation.findNavController(textView);
Bundle bundle = new Bundle();
bundle.putString("userID", userID);
navController.navigate(R.id.action_global_fragment_userDetail,bundle);
}else {
Log.e("API","@信息读取失败1");
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("API","@信息读取失败2");
}
}
);
Log.i("HTML","点击" + urlSpan.getURL());
queue.add(stringRequest);
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
ds.setColor(Color.parseColor("#1111ff")); // 超链接颜色
}
};
clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
clickableHtmlBuilder.removeSpan(urlSpan);
}
static public CharSequence getClickableHtml(String html, Context context, TextView textView) {
Spanned spannedHtml = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY,
new MyImageGetter(context,textView), null);
SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
for (final URLSpan span : urls) {
// 判断一下链接是否需要处理
if (span.getURL().substring(0,3).equals("/n/")){
setLinkClickable(clickableHtmlBuilder, span,context,textView);
}
}
return clickableHtmlBuilder;
}
}