Android Glidejava
Glide是Android圖片的加載與緩存的一個開源的框架。在咱們的app中,當手機本地圖片比較少的時候能夠較快的加載,可是當圖片較多時,圖片加載的時間就大大增長,另外因爲圖片的大小不是都同樣的,因此咱們使用Glide能夠使全部圖片在圖片列表中多能夠以相同的大小顯示給咱們,以達到美觀的效果。緩存
Glide的jar包導入lib文件夾下:app
import com.bumptech.glide.Glide;
Glide框架的使用方法:框架
Glide.with(context).load(本地資源).into(imageview);
部分代碼:ide
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; }