您的位置:首页技术文章
文章详情页

android - 用textview显示html时如何写imagegetter获取网络图片

浏览:46日期:2024-03-01 10:43:38

问题描述

项目需要实现图文混排,后台给出来的文本是html格式的,ui要求需要调整行间距,webview可以显示各种标签,但是无法调整行间距,试着往span中添加line-height也失败了,而且webview无法调整内边距,且webview中的内容可以滑动,因此不太符合我们的要求最后决定还是使用textview来实现,这样可以调整各种样式,但是在写imagegetter的时候遇到一些问题搜索了很久,都只是搜索到一些显示本地图片没有显示网络图片,网络图片的大致方向也是要保存到本地之后再显示但是在保存的时候会有一些问题,我保存时不知道为什么有ioexception关于imagegetter不知道有没有什么其他的思路

private Html.ImageGetter imageGetter = new Html.ImageGetter() { @Override public Drawable getDrawable(String source) {String url = getApplicationContext().getExternalCacheDir().getPath() + '/image';File dir = new File(url);if (dir.exists()) { Drawable drawable = Drawable.createFromPath(url+source); if (drawable != null){return drawable; }}loadPic(source);return null; }};private void loadPic(final String source){ x.image().loadDrawable(source, ImageOptions.DEFAULT,new Callback.CommonCallback<Drawable>(){@Overridepublic void onSuccess(Drawable result) { super.onSuccess(result); saveImage(source,result,getApplicationContext()); textview.setText(Html.fromHtml(content,imageGetter,null));} });}private void saveImage(String name,Drawable result, Context context) { Bitmap bit = ((BitmapDrawable) result).getBitmap(); String url = context.getExternalCacheDir().getPath() + '/image'; File dir = new File(url); if (!dir.exists()) {dir.mkdirs(); } File file = new File(dir.getAbsolutePath(),name); if (file.exists()) {return url+name; } try {//这里会出现ioexceptionFileOutputStream fos = new FileOutputStream(file);bit.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();return url+name; } catch (IOException e) {e.printStackTrace();return null; }}

这是我的代码,不知道又没有什么其他的好方法解决

问题解答

回答1:

http://git.oschina.net/zzhouj...

在这里找到一个,稍微修改一下可以使用,需要配置picasso,之后如果想到其他可以修改的方法会贴上来

回答2:

https://github.com/Sufficient...有这样一个工具,但并不完美。图片可以自动从网络加载,但是没有本地缓存,而且加载的过程也是异步的,所以你布局的高度会变得不确定,如果是在listview中的话不推荐使用。

回答3:

之前用过这个,还不错,也有默认的imagegetter方案,可以参考下。https://github.com/Sufficient...

回答4:

这还有一个https://github.com/angebagui/...

标签: HTML
相关文章: