android textView 异步加载html中的图片,android 4.0上出现图片重叠文本
来源:互联网 发布:把excel 数据导入jira 编辑:程序博客网 时间:2024/04/28 18:03
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
- android textView 异步加载html中的图片,android 4.0上出现图片重叠文本
- android textView 加载HTML 异步加载网络图片
- android textView 加载HTML 异步加载网络图片
- TextView加载Html文本中的图片
- Android点滴---TextView,RadioButton 设置 HTML文本,加载网络图片
- Android点滴---TextView,RadioButton 设置 HTML文本,加载网络图片
- Android TextView加载html图片详解
- Android TextView Html加载图片Glide
- android异步图片加载中的图片缓存
- TextView异步加载HTML格式数据中的图片(解决4.0以上主线程加载失败)
- TextView加载Html文本显示图片问题
- Android在TextView中显示html标签以及异步加载网页图片
- Android从网络上 异步加载图片
- android异步加载图片
- android 异步加载图片
- android异步加载图片
- android异步加载图片
- Android 异步加载图片
- iOS倒计时
- 我开发的OSX mac应用能在xcode里面运行却无法在外部运行。求解。
- zipalign的问题.
- Android Studio导入eclipse项目编译出错
- Xcode编译问题
- android textView 异步加载html中的图片,android 4.0上出现图片重叠文本
- android系统的Spinner真的不好看,你遇到这样的显示问题吗?
- 一个Fragment练习中碰到的问题
- Azure云计算概述
- android接入微信登录点击授权登录后如何接收微信的返回数据
- 大神们,有人做过车牌识别吗?不不会识别技术啊~
- 在重写的onMeasure()函数中,getMeasured的返回值为0,怎么解决?
- IOS 自动适配的代码 有点问题
- Select Html控件后台控制问题