带时间轴的图片选择器

前段时间整理项目,把图片选择器分出来整理成了moudle,现分享出来供大家参考。

首先上图

首先要将图片宽高一致

public class SquareLayout extends RelativeLayout {
    public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareLayout(Context context) {
        super(context);
    }

    @SuppressWarnings("unused")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

        // Children are just made to fill our space.
        int childWidthSize = getMeasuredWidth();
        int childHeightSize = getMeasuredHeight();
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

在以上自定义布局中定义图片大小

导入base.jar,实现TrustyGridSimpleAdapter中getheaderView方法来添加时间戳

public class ImageAdapter extends BaseAdapter implements TrustyGridSimpleAdapter {
    private Context mContext;

    private ArrayList<ImageItem> fileInfo;
    private ArrayList<ImageItem> selectedDataList;
    private DisplayMetrics dm;

    public ImageAdapter(Context mContext, ArrayList<ImageItem> fileInfo, ArrayList<ImageItem> selectedDataList) {
        this.mContext = mContext;
        this.fileInfo = fileInfo;

        this.selectedDataList = selectedDataList;
        dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay()
                .getMetrics(dm);
    }

    public void setData(ArrayList<ImageItem> fileInfo) {
        this.fileInfo = fileInfo;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (fileInfo != null && fileInfo.size() > 0) {
            count = fileInfo.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        ImageView ivImage;
        public CheckBox isSelect;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_image, null);

            viewHolder.ivImage = (ImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.isSelect = (CheckBox) convertView
                    .findViewById(R.id.isSelect);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final ImageItem item = fileInfo.get(position);
        if (item.isCamera) {
            viewHolder.isSelect.setVisibility(View.GONE);
            // ImageLoader.getInstance().displayImage("drawable://"+R.drawable.ic_camera,
            // holder.img);
            viewHolder.ivImage.setImageResource(R.drawable.ic_camera);
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    mOnItemClickListener.onCameraClick();
                }
            });
            return convertView;
        } else {

            viewHolder.isSelect.setVisibility(View.VISIBLE);
        }

        if (fileInfo != null && fileInfo.size() > 0) {

            File file = new File(fileInfo.get(position).imagePath);
            Glide.with(mContext).load(file)
                    .into(viewHolder.ivImage);
        }


        /*for (int i = 0; i < selectedDataList.size(); i++) {
            if (selectedDataList.get(i).getImagePath().equals(fileInfo.get(position).getImagePath())) {
                viewHolder.isSelect.setChecked(true);
            } else {

            }
        }*/

        final CheckBox isSelectBox = viewHolder.isSelect;

        viewHolder.isSelect.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (fileInfo != null && mOnItemClickListener != null
                        && position < fileInfo.size()) {
                    mOnItemClickListener.onSelectClick(position,
                            isSelectBox.isChecked(), isSelectBox);
                }
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (fileInfo != null && mOnItemClickListener != null
                        && position < fileInfo.size()) {
                    mOnItemClickListener.onItemClick(position,
                            isSelectBox.isChecked(), isSelectBox, fileInfo.get(position));
                }
            }
        });


        return convertView;
    }

    class HeaderViewHolder {
        public TextView tvTimeHeader;
    }

    @Override
    public View getHeaderView(final int position, View convertView, ViewGroup viewGroup) {

        HeaderViewHolder mHeadViewHolder = null;
        if (convertView == null) {
            mHeadViewHolder = new HeaderViewHolder();
            convertView = View.inflate(mContext, R.layout.item_time_header, null);

            mHeadViewHolder.tvTimeHeader = (TextView) convertView.findViewById(R.id.tv_time_header);

            convertView.setTag(mHeadViewHolder);
        } else {
            mHeadViewHolder = (HeaderViewHolder) convertView.getTag();
        }

        if (position == 0) {
            mHeadViewHolder.tvTimeHeader.setText("拍照");
        } else {
            mHeadViewHolder.tvTimeHeader.setText(fileInfo.get(position).getDate());
        }

        return convertView;
    }

    @Override
    public long getHeaderId(int i) {
        // File file = new File(fileInfo.get(i).getFilePath());
        return getTimeId(fileInfo.get(i).getDate());
        //  return getTimeId(strToDateLong(file.lastModified()));
    }

    public long getTimeId(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date mDate = null;

        try {
            mDate = sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }
        return mDate.getTime();
    }

    public String strToDateLong(long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }

    public interface OnItemClickListener {
        public void onSelectClick(int position, boolean isChecked,
                                  CheckBox checkBox);

        public void onItemClick(int position, boolean isChecked,
                                CheckBox checkBox, ImageItem imageItem);

        public void onCameraClick();
    }

}  

下载地址   点击打开链接

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页