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

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);
}

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值