ImageSpan 图文混排异步下载 demo.

有时候需要图文混排,需要用到ImageSpan,由于是图片所以要用到异步处理.此demo
class  MyAdapter extends BaseAdapter{
		List<JSONArray> list = new ArrayList<JSONArray>();
		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<String,Drawable> drawCache = new LruCache<String, Drawable>(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<Object,Void,Drawable>(){
						
						String mPath;//下载地址
						SpannableStringBuilder builder;//需要更新的builder
						WeakReference<TextView> weakReference;//textview对象
						int[] indexs;//imagespan所在的索引位置
						
						@Override
						protected Drawable doInBackground(Object... params) {//http 执行下载
							
							mPath = (String)params[0];
							builder = (SpannableStringBuilder)params[1];
							weakReference = new WeakReference<TextView>((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});
			}
		}
	}

你可能感兴趣的:(ImageSpan 图文混排异步下载 demo.)