当前位置: 代码迷 >> Android >> Android图片浏览器:在对话框上兑现图片浏览
  详细解决方案

Android图片浏览器:在对话框上兑现图片浏览

热度:41   发布时间:2016-05-01 19:11:31.0
Android图片浏览器:在对话框上实现图片浏览

前段时间弄了2个Gallery跟ImageSwitcher实现了图片浏览。

今天介绍一种在对话框上实现图像选择。

先声明,是借鉴别人的做法,觉得确实很好。

实现的效果如下:




部分关键代码如下:

对话框的设置:

重点是

view plain
  1. setView(imageChooseView)??
view plain
  1. 这个属性??

?

view plain
  1. public?void?initImageChooseDialog()?{??
  2. ????if(imageChooseDialog?==?null)?{??
  3. ????????AlertDialog.Builder?builder?=?new?AlertDialog.Builder(this);??
  4. ????????builder.setTitle("请选择图像")??
  5. ????????.setView(imageChooseView).setPositiveButton("确定",?new?DialogInterface.OnClickListener()?{??
  6. ????????????@Override??
  7. ????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
  8. ????????????????imageChanged?=?true;??
  9. ????????????????previousImagePosition?=?currentImagePosition;??
  10. ????????????????imageButton.setImageResource(images[currentImagePosition%images.length]);??
  11. ????????????}??
  12. ????????})??
  13. ????????.setNegativeButton("取消",?new?DialogInterface.OnClickListener()?{??
  14. ??????????????
  15. ????????????@Override??
  16. ????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
  17. ????????????????currentImagePosition?=?previousImagePosition;??
  18. ??????????????????
  19. ????????????}??
  20. ????????});??
  21. ????????imageChooseDialog?=?builder.create();??
  22. ????}??
  23. }??


?

加载图片的方法:

view plain
  1. public?void?loadImage()?{??
  2. ????if(imageChooseView?==?null)?{??
  3. ????????LayoutInflater?li?=?LayoutInflater.from(AddNew.this);??
  4. ????????imageChooseView?=?li.inflate(R.layout.imageswitch,?null);??
  5. ??????????
  6. ????????//通过渲染xml文件,得到一个视图(View),再拿到这个View里面的Gallery??
  7. ????????gallery?=?(Gallery)imageChooseView.findViewById(R.id.gallery);??
  8. ????????//为Gallery装载图片??
  9. ????????gallery.setAdapter(new?ImageAdapter(this));??
  10. ????????gallery.setSelection(images.length/2);??
  11. ????????is?=?(ImageSwitcher)imageChooseView.findViewById(R.id.imageswitch);??
  12. ????????is.setFactory(this);??
  13. ????????is.setInAnimation(AnimationUtils.loadAnimation(this,?android.R.anim.fade_in));??
  14. ????????//卸载图片的动画效果??
  15. ????????is.setOutAnimation(AnimationUtils.loadAnimation(this,?android.R.anim.fade_out));??
  16. ????????gallery.setOnItemSelectedListener(new?OnItemSelectedListener(){??
  17. ??
  18. ????????????@Override??
  19. ????????????public?void?onItemSelected(AdapterView<?>?arg0,?View?arg1,??
  20. ????????????????????int?arg2,?long?arg3)?{??
  21. ????????????????//当前的头像位置为选中的位置??
  22. ????????????????currentImagePosition?=?arg2;??
  23. ????????????????//为ImageSwitcher设置图像??
  24. ????????????????is.setImageResource(images[arg2?%?images.length]);??
  25. ??????????????????
  26. ????????????}??
  27. ????????????@Override??
  28. ????????????public?void?onNothingSelected(AdapterView<?>?arg0)?{??
  29. ??????????????????
  30. ????????????}});??
  31. ????}??
  32. ??????
  33. }??


重点,adapter的写法:

?

view plain
  1. class?ImageAdapter?extends?BaseAdapter?{??
  2. ??
  3. ????????private?Context?context;??
  4. ??????????
  5. ????????public?ImageAdapter(Context?context)?{??
  6. ????????????this.context?=?context;??
  7. ????????}??
  8. ??????????
  9. ????????@Override??
  10. ????????public?int?getCount()?{??
  11. ????????????return?Integer.MAX_VALUE;??
  12. ????????}??
  13. ??
  14. ????????@Override??
  15. ????????public?Object?getItem(int?position)?{??
  16. ????????????return?position;??
  17. ????????}??
  18. ??
  19. ????????@Override??
  20. ????????public?long?getItemId(int?position)?{??
  21. ????????????return?position;??
  22. ????????}??
  23. ??
  24. ??????????
  25. ????????/**?
  26. ?????????*?gallery从这个方法中拿到image?
  27. ?????????*/??
  28. ????????@Override??
  29. ????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
  30. ????????????ImageView?iv?=?new?ImageView(context);??
  31. ????????????iv.setImageResource(images[position%images.length]);??
  32. ????????????iv.setAdjustViewBounds(true);??
  33. ????????????iv.setLayoutParams(new?Gallery.LayoutParams(80,80));??
  34. ????????????iv.setPadding(15,?10,?15,?10);??
  35. ????????????return?iv;??
  36. ??????????????
  37. ????????}??
  38. ??????????
  39. ????}??
  40. ??
  41. ??????
view plain
  1. ??
view plain
  1. //这个是重写的的方法,我给加上注释。我是百度了很久也没找到这个方法的解释。只要对着有道看的源码~~??TAT??
  2. ??????
  3. ????@Override??
  4. ????public?View?makeView()?{??
  5. ????????ImageView?view?=?new?ImageView(this);??
  6. ????????view.setBackgroundColor(0xff000000);//设置背景边框颜色???
  7. ????????view.setScaleType(ScaleType.FIT_CENTER);//设置?显示的类型??居中显示??
  8. ????????view.setLayoutParams(new?ImageSwitcher.LayoutParams(90,90));//设置显示的图片大小??
  9. ????????return?view;??
  10. ????}??

?


<!--EndFragment-->
  相关解决方案