点击蓝色选择图片
public class SelectorPictureGridView extends GridView {
private SelectorPictureAdapter selectorPictureAdapter;
private int spacing = 8;
private int numColumns = 3;
public SelectorPictureGridView(Context context) {
this(context, null);
}
public SelectorPictureGridView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SelectorPictureGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setNumColumns(numColumns);
setGravity(Gravity.CENTER);
spacing = px2dp(spacing);
setVelocityScale(spacing);
setHorizontalSpacing(spacing);
setMargins(this, spacing, spacing, spacing, spacing);
}
private int getItemSize(){
DisplayMetrics dm = getResources().getDisplayMetrics();
int width = dm.widthPixels;
return width / numColumns - (spacing * 4);
}
private int px2dp(float pxValue) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
private void setMargins(View v, int l, int t, int r, int b) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
p.setMargins(l, t, r, b);
v.requestLayout();
}
}
@Override
public void setAdapter(ListAdapter adapter) {
// 不做处理
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
selectorPictureAdapter = new SelectorPictureAdapter(new ArrayList<Uri>());
SelectorPictureGridView.super.setAdapter(selectorPictureAdapter);
selectorPictureAdapter.addPicture(Uri.EMPTY);
}
public void setSelectorPictureListener(View.OnClickListener onClickListener) {
selectorPictureAdapter.setSelectorPictureListener(onClickListener);
}
public void addPicture(Uri uri) {
selectorPictureAdapter.addPicture(uri);
}
public List<Uri> getPictures(){
return selectorPictureAdapter.getPictures();
}
private class SelectorPictureAdapter extends BaseAdapter {
private final int PICTURE_TYPE = 1;
private final int SELECTOR_TYPE = 0;
private final List<Uri> mUriList;
private OnClickListener onClickListener;
SelectorPictureAdapter(List<Uri> uriList) {
this.mUriList = uriList;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return SELECTOR_TYPE;
}
return PICTURE_TYPE;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mUriList.size();
}
public void addPicture(Uri uri) {
mUriList.add(uri);
notifyDataSetChanged();
}
public List<Uri> getPictures(){
return mUriList.subList(1, mUriList.size());
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
PictureViewHolder pictureViewHolder = null;
SelectorViewHolder selectorViewHolder = null;
if (convertView == null) {
pictureViewHolder = new PictureViewHolder();
selectorViewHolder = new SelectorViewHolder();
switch (type) {
case SELECTOR_TYPE:
View selectorView = new View(parent.getContext());
selectorView.setLayoutParams(new GridView.LayoutParams(getItemSize(),
getItemSize()));
convertView = selectorView;
selectorViewHolder.selectorView = selectorView;
convertView.setTag(selectorViewHolder);
break;
case PICTURE_TYPE:
ImageView imageView = new ImageView(parent.getContext());
imageView.setLayoutParams(new GridView.LayoutParams(getItemSize(),
getItemSize()));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
convertView = imageView;
pictureViewHolder.pictureImageView = (ImageView) convertView;
convertView.setTag(pictureViewHolder);
break;
}
} else {
switch (type) {
case SELECTOR_TYPE:
selectorViewHolder = (SelectorViewHolder) convertView.getTag();
break;
case PICTURE_TYPE:
pictureViewHolder = (PictureViewHolder) convertView.getTag();
break;
}
}
switch (type) {
case SELECTOR_TYPE:
selectorViewHolder.selectorView.setBackgroundColor(Color.BLUE);
selectorViewHolder.selectorView.setOnClickListener(onClickListener);
break;
case PICTURE_TYPE:
pictureViewHolder.pictureImageView.setImageURI(mUriList.get(position));
break;
}
return convertView;
}
public void setSelectorPictureListener(View.OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
/**
* 选择照片
*/
class SelectorViewHolder {
View selectorView;
}
/**
* 显示的照片
*/
class PictureViewHolder {
ImageView pictureImageView;
}
}
}
网友评论