android textView 异步加载html中的图片,android 4.0上出现图片重叠文本

woaimmwwyy 2014-02-20 09:19:04
Spanned spanned = Html.fromHtml(_content, new MyImageGetter(this,
text_content), new MyTagHandler(this));
text_content.setText(spanned);
text_content.setMovementMethod(LinkMovementMethod.getInstance());


public class MyImageGetter implements ImageGetter {

private Context context;
private TextView tv;

public MyImageGetter(Context context, TextView tv) {
this.context = context;
this.tv = tv;
}

@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
// 将source进行MD5加密并保存至本地
String imageName = Common.md5(source);
String sdcardPath = Environment.getExternalStorageDirectory()
.toString(); // 获取SDCARD的路径
// 获取图片后缀名
String[] ss = source.split("\\.");
String ext = ss[ss.length - 1];

// 最终图片保持的地址
String savePath = sdcardPath + "/" + context.getPackageName() + "/"
+ imageName + "." + ext;

File file = new File(savePath);
if (file.exists()) {
// 如果文件已经存在,直接返回
Drawable drawable = Drawable.createFromPath(savePath);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
}

// 不存在文件时返回默认图片,并异步加载网络图片
Resources res = context.getResources();
URLDrawable drawable = new URLDrawable(
res.getDrawable(R.drawable.defualt_image));
new ImageAsync(drawable).execute(savePath, source);
return drawable;

}

private class ImageAsync extends AsyncTask<String, Integer, Drawable> {

private URLDrawable drawable;

public ImageAsync(URLDrawable drawable) {
this.drawable = drawable;
}

@Override
protected Drawable doInBackground(String... params) {
// TODO Auto-generated method stub
String savePath = params[0];
String url = params[1];

InputStream in = null;
try {
// 获取网络图片
HttpGet http = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = (HttpResponse) client.execute(http);
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(
response.getEntity());
in = bufferedHttpEntity.getContent();

} catch (Exception e) {
try {
if (in != null)
in.close();
} catch (Exception e2) {
// TODO: handle exception
}
}

if (in == null)
return drawable;

try {
File file = new File(savePath);
String basePath = file.getParent();
File basePathFile = new File(basePath);
if (!basePathFile.exists()) {
basePathFile.mkdirs();
}
file.createNewFile();
FileOutputStream fileout = new FileOutputStream(file);
byte[] buffer = new byte[4 * 1024];
while (in.read(buffer) != -1) {
fileout.write(buffer);
}
fileout.flush();

Drawable mDrawable = Drawable.createFromPath(savePath);
return mDrawable;
} catch (Exception e) {
// TODO: handle exception
}
return drawable;
}

@Override
protected void onPostExecute(Drawable result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result != null) {
drawable.setDrawable(result);
tv.setText(tv.getText()); // 通过这里的重新设置 TextView 的文字来更新UI
}
}

}

public class URLDrawable extends BitmapDrawable {

private Drawable drawable;

public URLDrawable(Drawable defaultDraw) {
setDrawable(defaultDraw);
}

private void setDrawable(Drawable nDrawable) {
drawable = nDrawable;
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
}

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
drawable.draw(canvas);
}

}
}
...全文
901 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kevin_Rosen 2015-05-26
  • 打赏
  • 举报
回复

这里显示方法是私有的

楼主回调方法里调用的是父类的方法?
  • 打赏
  • 举报
回复
楼主 有木有解决图片重复啊!!QQ980322048 加我给我说一下!
莐嶵De忄 2014-08-31
  • 打赏
  • 举报
回复
我也也遇到这方面的问题
grennhand 2014-07-20
  • 打赏
  • 举报
回复
不知道楼主解决了没有,解决了能共享一下么
星光下的微笑 2014-07-11
  • 打赏
  • 举报
回复
就是7楼的错误》。。
星光下的微笑 2014-07-11
  • 打赏
  • 举报
回复
我这一运行就崩了说什么内存溢出什么的 是怎么回事呀
东方小不 2014-06-04
  • 打赏
  • 举报
回复
楼主,你的代码有java.lang.StackOverflowError错误,定位在 drawable.draw(canvas); 160行这一句
大名喵喵 2014-04-28
  • 打赏
  • 举报
回复
tv.setText(tv.getText())这一句出的问题,图片多的时候,调用这个,会不停的调用imagegetter
大名喵喵 2014-04-28
  • 打赏
  • 举报
回复
图片多的时候,出现渲染层次过深的问题。。。有遇到的么?
哎,真难 2014-03-27
  • 打赏
  • 举报
回复
重叠文本?能贴下图,再发下demo
gaodengjie 2014-03-27
  • 打赏
  • 举报
回复
解决了?我也遇到了。
zx白 2014-03-19
  • 打赏
  • 举报
回复
楼主,求教啊
hsx49949046 2014-03-09
  • 打赏
  • 举报
回复
楼主解决了没?我也遇到同样问题。

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧