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/...
相關文章:
