android - 用textview显示html时如何写imagegetter获取网络图片
PHP中文网
PHP中文网 2017-04-17 17:40:14
0
4
507

项目需要实现图文混排,后台给出来的文本是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>(){
        @Override
        public 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 {
        //这里会出现ioexception
        FileOutputStream 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;
    }
}

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

PHP中文网
PHP中文网

认证0级讲师

모든 응답(4)
Ty80

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

여기서 찾았는데 약간만 수정하면 사용할 수 있습니다. Picasso를 구성해야 합니다. 다른 수정 방법이 생각나면 나중에 게시하겠습니다.

阿神

https://github.com/Sufficient...
그런 도구도 있지만 완벽하지는 않습니다.
이미지는 네트워크에서 자동으로 로드할 수 있지만 로컬 캐시가 없고 로드 프로세스가 비동기식이므로 레이아웃의 높이가 불확실해집니다. 목록 보기에 있는 경우 권장되지 않습니다.

迷茫

이전에 사용해 본 적이 있는데 꽤 좋습니다. 기본 이미지 게터 솔루션도 있으니 참고하시면 됩니다.
https://github.com/충분...

小葫芦

여기 또 다른 https://github.com/angebagui/...

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!