博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片选择器
阅读量:5120 次
发布时间:2019-06-13

本文共 14055 字,大约阅读时间需要 46 分钟。

注释比较少。。。直接贴代码吧,主要有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
mImgs; private BasePopWindow popWindow; private ProgressDialog mProgressDialog; private File mCurrentDir; private int mMaxCount; private List
ListBean = new ArrayList
(); 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
mDirPaths = new HashSet
(); 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
mSelectedImg = new HashSet
(); private String mDirPath; //图片所在文件夹路径 private List
mImgPaths = new ArrayList
(); //图片名称的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
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
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
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
{ private LayoutInflater mInflater; private List
mDatas; public ListAdapter(Context context, List
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文件就省略了

转载于:https://www.cnblogs.com/smallJunJun/p/9373779.html

你可能感兴趣的文章
Eclipse相关集锦
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>
继承条款effecitve c++ 条款41-45
查看>>
HTML+CSS学习笔记(九)
查看>>
Java泛型的基本使用
查看>>
1076 Wifi密码 (15 分)
查看>>
rsync
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
图片等比例缩放及图片上下剧中
查看>>
【转载】Linux screen 命令详解
查看>>
background-clip,background-origin
查看>>
Android 高级UI设计笔记12:ImageSwitcher图片切换器
查看>>
Blog文章待看
查看>>
【Linux】ping命令详解
查看>>
对团队成员公开感谢博客
查看>>
java学习第三天
查看>>