最近项目中用到了这个功能,仿微信图片选择器。网上搜了很多,没找到合适的例子。就自己写了一个例子,由于时间匆匆,没有做缓存。
之前也写过这样的功能,都是用第三方的。这次一是因为时间紧,二是因为是在写好的代码的基础上添加功能,不想多改。所以自己就写了一个简单的。
先上布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridView"
android:numColumns="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
接着是项目代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 9900) {
switch (resultCode) {
case RESULT_OK:
Uri uri = data.getData();
list.add(uri.toString());
adapter.notifyDataSetChanged();
break;
case RESULT_CANCELED:
// finish();
break;
}
}
}
这是Adapter里面的判断
@Override
public int getCount() {
// TODO Auto-generated method stub
if (list.size() == 6) {
return 6;
} else {
return list.size() + 1;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = View.inflate(MainActivity.this,
R.layout.layout_gridview_item, null);
viewHolder.imageView = (ImageView) convertView
.findViewById(R.id.iv);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (position == list.size()) {
viewHolder.imageView.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_launcher));
viewHolder.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//打开相册
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 9900);
}
});
} else {
final String s = list.get(position).toString();
viewHolder.imageView.setImageURI(Uri.parse(list.get(position)));
viewHolder.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//点击预览,用的是全屏的dialog。
showPicDialog(s);
}
});
}
return convertView;
}
大图片预览
public void showPicDialog(final String string) {
dialog = new Dialog(this,R.style.Dialog_Fullscreen);
View view = View.inflate(this,R.layout.layout_dialog_item,null);
dialog.addContentView(view, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ImageView imageView = (ImageView) view.findViewById(R.id.dialog_iv);
view.findViewById(R.id.ht).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
view.findViewById(R.id.sc).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.remove(string);
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
imageView.setImageURI(Uri.parse(string));
dialog.show();
}
整体思路很简单,就是一个图片的展示,不过这里没有做缓存,一定要注意OOM!!!!