博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ImageSpan 图文混排异步下载 demo.
阅读量:6504 次
发布时间:2019-06-24

本文共 4555 字,大约阅读时间需要 15 分钟。

hot3.png

有时候需要图文混排,需要用到ImageSpan,由于是图片所以要用到异步处理.此demo
class  MyAdapter extends BaseAdapter{		List
list = new ArrayList
(); Context mContext; LayoutInflater mInflater; public MyAdapter(Context context) { this.mContext = context; this.mInflater = LayoutInflater.from(context); JSONArray jsonObject = new JSONArray();//模拟图片混排 jsonObject.put("http://e.hiphotos.baidu.com/album/w%3D2048/sign=c54faa473ac79f3d8fe1e3308e99cc11/7a899e510fb30f244ed503bbc995d143ad4b033e.jpg"); jsonObject.put("http://e.hiphotos.baidu.com/album/w%3D2048/sign=c54faa473ac79f3d8fe1e3308e99cc11/7a899e510fb30f244ed503bbc995d143ad4b033e.jpg"); for (int i = 0; i < 100; i++) { list.add(jsonObject); } } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null){ holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.list_item_layout,null); holder.textView = (TextView)convertView; convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } JSONArray array = list.get(position); SpannableStringBuilder builder = new SpannableStringBuilder(); holder.textView.setTag(R.id.listView1,builder); for (int i = 0; i < array.length(); i++) { try { String str = array.getString(i); //执行异步下载 mSpanAsyncLoad.displayImage(str, builder, holder.textView); } catch (JSONException e) { e.printStackTrace(); } } holder.textView.setText(builder); return convertView; } }
 
 
public static class ImageSpanAsyncLoad{		Context mContext;		Drawable mDrawable = null;//默认显示图片		final LruCache
drawCache = new LruCache
(100); public ImageSpanAsyncLoad(Context context) { mContext = context.getApplicationContext(); Resources resources = mContext.getResources(); mDrawable = resources.getDrawable(R.drawable.ic_launcher); mDrawable.setCallback(null); mDrawable.setBounds(0,0,mDrawable.getIntrinsicHeight(),mDrawable.getIntrinsicWidth()); } public void displayImage(String path,SpannableStringBuilder builder,TextView textView){ Drawable drawable = drawCache.get(path); boolean isNeedDown = false; if(drawable == null){//缓存无图片 isNeedDown = true; drawable = mDrawable; } ImageSpan imageSpan = new ImageSpan(drawable); SpannableString spannableString = new SpannableString(path); spannableString.setSpan(imageSpan,0,spannableString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.append(spannableString); if(isNeedDown){ int startIndex = builder.getSpanStart(imageSpan); int endIndex = builder.getSpanEnd(imageSpan); new AsyncTask
(){ String mPath;//下载地址 SpannableStringBuilder builder;//需要更新的builder WeakReference
weakReference;//textview对象 int[] indexs;//imagespan所在的索引位置 @Override protected Drawable doInBackground(Object... params) {//http 执行下载 mPath = (String)params[0]; builder = (SpannableStringBuilder)params[1]; weakReference = new WeakReference
((TextView)params[2]); indexs = (int[])params[3];//imagespan 的索引位置 Drawable drawable = mContext.getResources().getDrawable(R.drawable.ic_launcher); drawable.setBounds(0, 0, 50, 50); drawable.setCallback(null); if(drawable != null)drawCache.put(mPath,drawable); return drawable; } @Override protected void onPostExecute(Drawable drawable) {//更新到ui if(drawable == null)return; TextView textView = weakReference.get(); //如果不为null与当前的textview对象是等于需要更新的textview对象则进行更新 if(textView != null && (textView.getTag(R.id.listView1) == builder)){ ImageSpan imageSpan = new ImageSpan(drawable); SpannableString spannableString = new SpannableString(mPath); spannableString.setSpan(imageSpan, 0, spannableString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); builder.replace(indexs[0],indexs[1],spannableString);//直接替换之前位置的imagespan textView.setText(builder); } } }.execute(path,builder,textView,new int[]{startIndex,endIndex}); } } }

转载于:https://my.oschina.net/zorigin/blog/140951

你可能感兴趣的文章
android RecycleView Adapter简单封装
查看>>
PgSQL · 案例分享 · 递归收敛优化
查看>>
Dart的数据库操作
查看>>
Codeforces 591 B Rebranding【Codeforces Round #327 (Div. 2)】
查看>>
命名难,难于上青天
查看>>
做完和做好不一样
查看>>
APUE读书笔记-05标准输入输出库(7)
查看>>
23 第一周作业
查看>>
DNS解析偶尔延迟
查看>>
iOS打电话,发短信,发邮件,打开网址
查看>>
06-验证码-基本功能实现
查看>>
Java数据结构与算法(六) 希尔排序
查看>>
canvas学习笔记
查看>>
IntelliJ Idea下Go项目开启Debug调试
查看>>
elasticsearch安装步骤
查看>>
PHP获取Cookie模拟登录CURL(转)
查看>>
PHP-权限控制类(转)
查看>>
CSS3秘笈第三版涵盖HTML5学习笔记9~12章
查看>>
bzoj1044木棍分割
查看>>
leetcode-136-Single Number
查看>>