android 解析html 图文混排
来源:互联网 发布:狗狗听懂人说话知乎 编辑:程序博客网 时间:2024/05/29 10:07
public class MyImageGetter implements Html.ImageGetter { WeakReference<TextView> mTextViewReference; Context mContext; public MyImageGetter(Context context, TextView textView) { mContext = context.getApplicationContext(); mTextViewReference = new WeakReference<TextView>(textView); } @Override public Drawable getDrawable(String url) { URLDrawable urlDrawable = new URLDrawable(mContext); // 异步获取图片,并刷新显示内容 new ImageGetterAsyncTask(url, urlDrawable).execute(); return urlDrawable; } public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> { WeakReference<URLDrawable> mURLDrawableReference; String mUrl; public ImageGetterAsyncTask(String url, URLDrawable drawable) { mURLDrawableReference = new WeakReference<URLDrawable>(drawable); mUrl = url; } @Override protected Drawable doInBackground(String... params) { // 下载图片,并且使用缓存 try { Bitmap bitmap = Glide.with(mContext).load(mUrl).asBitmap().centerCrop().into(100,100).get(); BitmapDrawable bitmapDrawable = new BitmapDrawable(mContext.getResources(), bitmap); Rect bounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); if (mURLDrawableReference.get() != null) { mURLDrawableReference.get().setBounds(bounds); } bitmapDrawable.setBounds(bounds); return bitmapDrawable; } catch (Exception e) { e.printStackTrace(); return null; } } @Override protected void onPostExecute(Drawable result) { if (null != result) { if (mURLDrawableReference.get() != null) { mURLDrawableReference.get().drawable = result; } if (mTextViewReference.get() != null) { // 加载完一张图片之后刷新显示内容 mTextViewReference.get().setText(mTextViewReference.get().getText()); } } } } public class URLDrawable extends BitmapDrawable { protected Drawable drawable; public URLDrawable(Context context) { // 设置默认大小和默认图片 Rect bounds = new Rect(0, 0, 100, 100); setBounds(bounds); drawable = context.getResources().getDrawable(R.drawable.morentu); drawable.setBounds(bounds); } @Override public void draw(Canvas canvas) { if (drawable != null) { drawable.draw(canvas); } } }}
public class MyTagHandler implements Html.TagHandler { private Context mContext; public MyTagHandler(Context context) { mContext = context.getApplicationContext(); } @Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { // 处理标签<img> if (tag.toLowerCase(Locale.getDefault()).equals("img")) { // 获取长度 int len = output.length(); // 获取图片地址 ImageSpan[] images = output.getSpans(len-1, len, ImageSpan.class); String imgURL = images[0].getSource(); // 使图片可点击并监听点击事件 output.setSpan(new ClickableImage(mContext, imgURL), len-1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } private class ClickableImage extends ClickableSpan { private String url; private Context context; public ClickableImage(Context context, String url) { this.context = context; this.url = url; } @Override public void onClick(View widget) { // 进行图片点击之后的处理 ToastUtil.showToast(context,"111",0); } }}
MyImageGetter imageGetter = new MyImageGetter(this,text_reward_description ); MyTagHandler tagHandler = new MyTagHandler(this); text_reward_description.setText(Html.fromHtml(task.getAward_desc(),imageGetter,tagHandler)); text_reward_description.setMovementMethod(LinkMovementMethod.getInstance());
阅读全文
0 0
- android 解析html 图文混排
- Android自动解析html带图片,实现图文混排
- Android/Html图文混排
- Android TextView加载Html图文混排
- android.text.Html源码解析-再也不用担心图文混排什么的了
- android图文混排
- Android图文混排
- Android 图文混排
- android图文混排
- Android图文混排
- Android图文混排
- Android 图文混排
- android图文混排
- Android ListView - 图文混排
- android 显示图文混排
- android TextView图文混排
- android TextView图文混排
- Android Button1.图文混排
- linux双网卡负载均衡bonding使用方法
- Java面试题集(51-70)
- 计蒜客-跳跃游戏
- UVa 10680
- CSDN博客转载方法
- android 解析html 图文混排
- 需要class, interface或enum
- oracle的分析函数over及开窗函数
- Running migrations: No migrations to apply.(django不能创建数据库中的表的问题)
- android 为什么需要签名
- 正则表达式学习
- java实现Base64加密
- win10 小图标点击不了?解决方法
- windows8配置homestead及laravel开发环境