团队博客13

Android Glide

  Glide是Android图片的加载与缓存的一个开源的框架。在我们的app中,当手机本地图片比较少的时候可以较快的加载,但是当图片较多时,图片加载的时间就大大增加,另外由于图片的大小不是都一样的,所以我们使用Glide可以使所有图片在图片列表中多可以以相同的大小显示给我们,以达到美观的效果。

Glide的jar包导入lib文件夹下:

import com.bumptech.glide.Glide;

 Glide框架的使用方法:

Glide.with(context).load(本地资源).into(imageview);

 部分代码:

private int[] imageTo;
	private String[] imageFrom;
	private ViewBinder imageViewBinder;
	private List<?extends Map<String,?>> imageData;
	private LayoutInflater imageInflater;
	private Context context;
	public HashMap<Integer,Boolean> isSelected;
	public int resource;
	
	static class ViewHolder{  
        CheckBox selected;  
        TextView path;
        TextView date;
        TextView size;
        ImageView image;
    }  
	public HashMap<Integer,Boolean> getIsSelected()
	{
		return isSelected;
	}
	
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return super.getItem(position);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return super.getItemId(position);
	}
	//构造器
	public PictureAdapter(Context context, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		// TODO Auto-generated constructor stub

		this.context=context;
		this.resource=resource;
		imageData=data;
		imageFrom=from;
		imageTo=to;
		imageInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		init();
		
	}
    
	private void init()
	{
		isSelected=new HashMap<Integer,Boolean>();
		for(int i=0;i<imageData.size();i++)
		{
			isSelected.put(i, false);
		}
	}
	
	public View getView(final int position,View view,ViewGroup parent)
	{
		
		ViewHolder holder = null;
		if(null == holder)
		{
		   //总是新建一个ViewHolder对象,用来保存每一个listview条目的信息		   
		   holder = new ViewHolder();
		   if(null == view)
		   {
		       imageInflater = LayoutInflater.from(context);
		       view = imageInflater.inflate(resource, null);
		   }
		   holder.date= (TextView) view.findViewById(R.id.pictureDate);
		   holder.size = (TextView) view.findViewById(R.id.pictureSize);
		   holder.selected = (CheckBox) view.findViewById(R.id.choosePicture);
		   holder.path=(TextView)view.findViewById(R.id.picturePath);
		   holder.image=(ImageView)view.findViewById(R.id.image);
		}
		  
		Map<String, ?> map = imageData.get(position);
		if(null != map)
		{
		    String date=(String) map.get("date");
		    String size=(String)map.get("size");
		    String path=(String)map.get("path");
		    holder.date.setText(date);
		    holder.size.setText(size);
		    holder.path.setText(path);
		    Glide.with(context).load(new File(path)).into(holder.image);
		}
		  
		//添加checkBox监听
		holder.selected.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		   
		    @Override
		    public void onCheckedChanged(CompoundButton arg0, boolean isCheck)
		    {
		        if(isCheck)
		        {
		            isSelected.put(position, true);
		            //System.out.println("add checked=" + position);
		        }
		        else
		            if(!isCheck)
		            {
		                isSelected.put(position, false);
		                //System.out.println("remove checked=" + position);
		            }
		     }
		 });
		  
		//根据isSelected中记录的信息,设置checkbox的状态
		holder.selected.setChecked(isSelected.get(position));
		return view;    
	}	

  

posted @ 2016-09-13 22:00  JYJe族  阅读(134)  评论(0编辑  收藏  举报