android - 用textview显示html时如何写imagegetter获取网络图片
问题描述
项目需要实现图文混排,后台给出来的文本是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/...
相关文章:
1. python round 四舍五入?2. sql语句 - 如何在mysql中批量添加用户?3. javascript - 按钮链接到另一个网址 怎么通过百度统计计算按钮的点击数量4. 事务 - mysql共享锁lock in share mode的实际使用场景5. mysql - PHP定时通知、按时发布怎么做?6. mysql - 数据库建字段,默认值空和empty string有什么区别 1107. 怎么php怎么通过数组显示sql查询结果呢,查询结果有多条,如图。8. node.js - mysql如何通过knex查询今天和七天内的汇总数据9. mysql - JAVA怎么实现一个DAO同时实现查询两个实体类的结果集10. python - 请问这两个地方是为什么呢?
