注释比较少。。。直接贴代码吧,主要有popupWindow的实现,然后通过ContentResolver便利自己手机里的所有图片,直接贴代码吧
主Activity的实现
public class MainActivity extends Activity {
private GridView id_gridView;
private TextView dir_name;
private TextView dir_pic;
private RelativeLayout bottom;
private List<String> mImgs;
private BasePopWindow popWindow;
private ProgressDialog mProgressDialog;
private File mCurrentDir;
private int mMaxCount;
private List<Bean> ListBean = new ArrayList<Bean>();
private ImageAdapter mImgAdapter;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 0x110)
{
mProgressDialog.dismiss();
//绑定数据到View中
data2View();
initPopWindow();
}
}
};
protected void initPopWindow()
{
popWindow = new BasePopWindow(this,ListBean);
popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
lightOn();
}
});
popWindow.setOnDirSelectedListener(new BasePopWindow.OnDirSelectedListener() {
@Override
public void onSelected(Bean bean) {
mCurrentDir = new File(bean.getDir());
mImgs = Arrays.asList(mCurrentDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
if(filename.endsWith(".jpg")
||filename.endsWith(".jpeg")
||filename.endsWith(".png"))
return true;
return false;
}
}));
mImgAdapter = new ImageAdapter(MainActivity.this,mImgs,mCurrentDir.getAbsolutePath());
id_gridView.setAdapter(mImgAdapter);
dir_name.setText(bean.getName());
dir_pic.setText(mImgs.size()+"");
popWindow.dismiss();
}
});
}
//内容区域变亮
protected void lightOn()
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1.0f;
getWindow().setAttributes(lp);
}
//内容区域变暗
protected void lightOff()
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.3f;
getWindow().setAttributes(lp);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initDatas();
initEvent();
}
protected void data2View()
{
if(mCurrentDir == null)
{
Toast.makeText(this,"未扫描到任何图片",Toast.LENGTH_SHORT).show();
return;
}
mImgs = Arrays.asList(mCurrentDir.list());
mImgAdapter = new ImageAdapter(MainActivity.this, mImgs ,mCurrentDir.getAbsolutePath());
id_gridView.setAdapter(mImgAdapter);
dir_name.setText(mMaxCount + "");
dir_pic.setText(mCurrentDir.getName());
}
private void initView()
{
id_gridView = (GridView)findViewById(R.id.id_gridView);
bottom = (RelativeLayout)findViewById(R.id.bottom);
dir_name =(TextView) findViewById(R.id.dir_name);
dir_pic = (TextView)findViewById(R.id.dir_pic);
}
private void initEvent() {
bottom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popWindow.setAnimationStyle(R.style.dir_popwindow_anim);
popWindow.showAsDropDown(bottom,0,0);
lightOff();
}
});
}
//利用ContentProvider扫描手机中的所有照片
private void initDatas() {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Toast.makeText(this,"当前存储卡不可用",Toast.LENGTH_SHORT).show();
return;
}
mProgressDialog = ProgressDialog.show(this,null,"正在加载");
new Thread()
{
public void run()
{
Uri mImgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = MainActivity.this.getContentResolver();
Cursor cursor;
cursor = cr.query(mImgUri, null, MediaStore.Images.Media.MIME_TYPE
+ "=? or " + MediaStore.Images.Media.MIME_TYPE
+ "=?", new String[]
{"image/jpeg", "image/png"},
MediaStore.Images.Media.DATE_MODIFIED);
Set<String> mDirPaths = new HashSet<String>();
while(cursor.moveToNext())
{
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
File parenFile = new File(path).getParentFile();
if(parenFile == null)
continue;
String dirPath = parenFile.getAbsolutePath();
Bean bean = null;
if(mDirPaths.contains(dirPath))
{
continue;
}else
{
mDirPaths.add(dirPath);
bean = new Bean();
bean.setDir(dirPath);
bean.setFirstImgPath(path);
}
if(parenFile.list() == null)
continue;
int picSize = parenFile.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
if(filename.endsWith(".jpg")||filename.endsWith(".jpeg")||filename.endsWith(".png"))
return true;
return false;
}
}).length;
bean.setCount(picSize);
ListBean.add(bean);
if(picSize > mMaxCount)
{
mMaxCount = picSize;
mCurrentDir = parenFile;
}
}
cursor.close();
//扫描完成,释放临时变量的内存
//mDirPaths = null;
//通知handler扫描图片完成
mHandler.sendEmptyMessage(0x110);
}
}.start();
}
}
自己定义的Bean类
public class Bean {
private String dir;
private String firstImgPath;
private String name;
private int count;
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
int lastIndexOf = this.dir.lastIndexOf("/");
this.name = this.dir.substring(lastIndexOf);
}
public String getFirstImgPath() {
return firstImgPath;
}
public void setFirstImgPath(String firstImgPath) {
this.firstImgPath = firstImgPath;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
ImageAdapter类
public class ImageAdapter extends BaseAdapter
{
private Set<String> mSelectedImg = new HashSet<String>();
private String mDirPath; //图片所在文件夹路径
private List<String> mImgPaths = new ArrayList<String>(); //图片名称的List集合
private LayoutInflater mInflater;
private Context mContext;
@Override
public int getCount() {
return mImgPaths.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.item,parent,false);
holder = new ViewHolder();
holder.img = (ImageView)convertView.findViewById(R.id.item_image);
holder.img_btn= (ImageButton)convertView.findViewById(R.id.item_selected);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
//重置状态
holder.img.setImageResource(R.drawable.lableperson);
holder.img_btn.setImageResource(R.drawable.the1no);
holder.img.setColorFilter(null);
final String filePath = mDirPath+"/"+mImgPaths.get(position);
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//已经被选择,再次点击就会被取消
if(mSelectedImg.contains(filePath))
{
mSelectedImg.remove(filePath);
holder.img.setColorFilter(null);
holder.img_btn.setImageResource(R.drawable.the1no);
}else //未被选择,点击会被选择
{
mSelectedImg.add(filePath);
holder.img.setColorFilter(Color.parseColor("#77000000"));
holder.img_btn.setImageResource(R.drawable.the1yes);
}
// notifyDataSetChanged();去掉解决闪屏的问题
}
});
if(mSelectedImg.contains(filePath))
{
holder.img.setColorFilter(Color.parseColor("#77000000"));
holder.img_btn.setImageResource(R.drawable.the1yes);
}
ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(mDirPath+"/"+mImgPaths.get(position),holder.img);
return convertView;
}
@Override
public Object getItem(int position) {
return mImgPaths.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public ImageAdapter(Context context , List<String> mDatas , String dirPath)
{
this.mContext=context;
this.mImgPaths=mDatas;
this.mDirPath=dirPath;
mInflater=LayoutInflater.from(context);
}
private class ViewHolder
{
public ImageView img;
public ImageButton img_btn;
}
}
popWindow类的实现
public class BasePopWindow extends PopupWindow { private int mWidth; private int mHeight; private View mConvertView; private List<Bean> mDatas; private ListView mListView; private Context mContext; public interface OnDirSelectedListener { void onSelected(Bean bean); } public OnDirSelectedListener mListener; public void setOnDirSelectedListener(OnDirSelectedListener mListener) { this.mListener = mListener; } public BasePopWindow(Context context, List<Bean> datas) { calWidHeight(context); mConvertView = LayoutInflater.from(context).inflate(R.layout.popwindow,null); mDatas = datas; setContentView(mConvertView); setWidth(mWidth); setHeight(mHeight); setFocusable(true); setTouchable(true); setOutsideTouchable(true); setBackgroundDrawable(new BitmapDrawable()); setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_OUTSIDE) { dismiss(); return true; } return false; } }); initViews(context); initEvents(); } private void initViews(Context context) { mListView = (ListView)mConvertView.findViewById(R.id.list_pop); mListView.setAdapter(new ListAdapter(context ,mDatas)); } private void initEvents() { mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(mListener != null) { mListener.onSelected(mDatas.get(position)); } } }); } //计算popWindow的宽度和高度 private void calWidHeight(Context context) { WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); mWidth = outMetrics.widthPixels; mHeight = (int)(outMetrics.heightPixels*0.7); } private class ListAdapter extends ArrayAdapter<Bean> { private LayoutInflater mInflater; private List<Bean> mDatas; public ListAdapter(Context context, List<Bean> objects) { super(context, 0, objects); mInflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.pop_item,parent,false); holder.mImg = (ImageView)convertView.findViewById(R.id.item_image_pop); holder.mDirName = (TextView)convertView.findViewById(R.id.item_name_pop); holder.mDirCount = (TextView)convertView.findViewById(R.id.item_count_pop); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } Bean bean = getItem(position); //重置 holder.mImg.setImageResource(R.drawable.the1no); ImageLoader.getInstance().loadImage(bean.getFirstImgPath(),holder.mImg); holder.mDirCount.setText(bean.getCount()+""); holder.mDirName.setText(bean.getName()); return convertView; } private class ViewHolder { ImageView mImg; TextView mDirName; TextView mDirCount; } } }xml文件就省略了