android写一个图片选择剖析

上传人:hs****ma 文档编号:494885981 上传时间:2022-10-07 格式:DOC 页数:10 大小:184KB
返回 下载 相关 举报
android写一个图片选择剖析_第1页
第1页 / 共10页
android写一个图片选择剖析_第2页
第2页 / 共10页
android写一个图片选择剖析_第3页
第3页 / 共10页
android写一个图片选择剖析_第4页
第4页 / 共10页
android写一个图片选择剖析_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《android写一个图片选择剖析》由会员分享,可在线阅读,更多相关《android写一个图片选择剖析(10页珍藏版)》请在金锄头文库上搜索。

1、android写一个图片选择可以达到的效果 第一个图片的位置放照相机,点击打开照相机 其余的是显示全部存储的图片,点击一次是查看大图,长按则是每张图片出现一个checkBox,可以进行选择下面是实例效果图Mai nActivity 类public class Mai nActivityexte ndsAppCompatActivityimpleme ntsAdapterView.O nl temClickListe ner,AdapterView.O nl temL on gClickListe ner,ImageAdapter.O nl mageCheckListe ner, View. O

2、n ClickListe ner private static final int CAMERA_CODE = 12;List fileList = new ArrayList();ImageAdapter adapter;GridView gvImage;TextView tvFinish;Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);initView();/弹出对话框,加

3、载数据loadData();private void initView() gvImage = (GridView) findViewById(R.id.gv_image); tvFinish = (TextView) findViewById(R.id.tv_finish);adapter = new ImageAdapter(this, fileList); adapter.setOnImageCheckListener(this); gvImage.setAdapter(adapter); gvImage.setOnItemClickListener(this); gvImage.set

4、OnItemLongClickListener(this);tvFinish.setOnClickListener(this);private ProgressDialog showProgressDialog() /弹出对话框ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle( 提示 );dialog.setMessage(”正在加载图片,请稍等。”); dialog.show();return dialog;private void loadData() final ProgressDialog dialog

5、= showProgressDialog();/开启线程new Thread() Overridepublic void run() super.run();/递归/从 sd 卡中获取所有图片 getFile(Environment.getExternalStorageDirectory(); runOnUiThread(new Runnable() Overridepublic void run() dialog.dismiss(); adapter.notifyDataSetChanged(););.start();public void getFile(File dir) /1. 获取子

6、目录File files = dir.listFiles();if (files = null)return;/集合或者数组去点 forfor (File file : files) if (file.isDirectory()getFile(file);else /加载图片if (file.getName().endsWith(.png) | file.getName().endsWith(.jpg) fileList.add(file);File cameraFile;/点击Overridepublic void onItemClick(AdapterView parent, View v

7、iew, int position, long id) if (position = 0) new/getAbsolutePath 返回的路径是没有 / cameraFile =/DCIM/File(Environment.getExternalStorageDirectory().getAbsolutePath() System.currentTimeMillis() + .png);/打开照相机Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); /照相机需要带数据intent.putExtra(MediaStore.EX

8、TRA_OUTPUT, Uri.fromFile(cameraFile); startActivityForResult(intent, CAMERA_CODE); else /打开大图 File file = fileList.get(position - 1); /带数据跳转到现实大图 Intent intent = new Intent(this, ShowBigImage.class); intent.putExtra(file, file); startActivity(intent);Override protected void onActivityResult(int requ

9、estCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data);Log.e(TAG, resultCode + );if (requestCode = CAMERA_CODE & resultCode = RESULT_OK) Log.e(TAG, (cameraFile.exists() + );fileList.add(0, cameraFile); adapter.notifyDataSetChanged();/长按 Override public boolean on

10、ItemLongClick(AdapterView parent, View view, int position, long id) if (position = 0)return false;else adapter.open(position);return true;Overridepublic void onImageCheck(boolean b) /b 代表 适配器中 有没有勾选的值 tvFinish.setEnabled(b);Override public void onClick(View v) /需要知道有哪些数据被选中 /不能使用泛型, ArrayList 才实现了序列

11、化, List 没有实现 ArrayList resultList = new ArrayList(); /通过适配器中的 为 true 的 选中的项来加载 file SparseBooleanArray booleanArray = adapter.getBooleanArray(); for (int i = 0; i booleanArray.size(); i+) boolean isCheck = booleanArray.get(booleanArray.keyAt(i); if (isCheck) int position = booleanArray.keyAt(i); res

12、ultList.add(fileList.get(position - 1); Intent intent = new Intent(); intent.putExtra(list, resultList);/返回数据 setResult(RESULT_OK, intent);finish();ImageAdapter 类public class ImageAdapter extends ListItemAdapter private boolean select = false;public void open(int posisiont) select = true; booleanArr

13、ay.put(posisiont, true); if (onImageCheckListener != null) onImageCheckListener.onImageCheck(true);this.notifyDataSetChanged();public void close() select = false; booleanArray.clear(); notifyDataSetChanged();/position/HashMap map = new HashMap();private SparseBooleanArray booleanArray = new SparseBo

14、oleanArray();public SparseBooleanArray getBooleanArray() return booleanArray;public ImageAdapter(Context context, List list) super(context, list);Overridepublic int getCount() /多出来的就是照相机return super.getCount() + 1;/ Override/public View getView(int position, View convertView, ViewGroup parent) /if (

15、convertView = null) /ImageView iv = new ImageView(mContext);/iv.setScaleType(ImageView.ScaleType.CENTER_CROP);/iv.setBackgroundColor(Color.argb(0xFF, 0x07, 0x05, 0x18);/int width = mContext.getResources().getDisplayMetrics().widthPixels / 3 - 2;/GridView.LayoutParams params = new GridView.LayoutParams(width

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号