一 自定义相册
结合Glide图片库,加载显示本地图片,并可以实现单选,多选,预览功能。特点
- 加载最近新增图片,GridView显示
- 分文件夹选择图片
- 支持单选,多选(最大9张)
- 支持大图预览
以库的形式保存,实际项目中导入PhotoSelector库使用。
二 九宫格显示图片
- 九宫格形式显示图片
- 点击预览大图
GirdView 设置
1 <com.zc.baselib.view.NoScrollGridView 2 android:id="@+id/gv_photo" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:layout_gravity="center" 6 android:layout_marginTop="@dimen/margin_min" 7 android:background="@null" 8 android:clickable="false" 9 android:listSelector="@android:color/transparent" 10 android:verticalSpacing="@dimen/margin_min_a" 11 android:numColumns="3"/>
自定义PhotoGridAdapter数据源
1 public class PhotoGridAdapter extends CommonListAdapter<ImageModel> { 2 3 private int itemWidth; 4 private int gridViewWidth; 5 private int horizentalNum = 3; 6 private AbsListView.LayoutParams itemLayoutParams; 7 8 public PhotoGridAdapter(Context context, List<ImageModel> mDatas) { 9 super(context, mDatas); 10 } 11 12 public PhotoGridAdapter(Context context, List<ImageModel> mDatas, int width) { 13 super(context, mDatas); 14 this.gridViewWidth = width; 15 setItemWidth(); 16 } 17 18 /** 19 * 设置每一个Item的宽高 = (GirdView宽度 - 行间距) / 每行显示个数 20 */ 21 public void setItemWidth() { 22 int horizentalSpace = mContext.getResources().getDimensionPixelSize(com.zc.photoselector.R.dimen.gridview_item_horizontalSpacing_4); 23 this.itemWidth = (gridViewWidth - (horizentalSpace * (horizentalNum - 1))) / horizentalNum; 24 // this.itemWidth = viewWidth / horizentalNum; 25 this.itemLayoutParams = new AbsListView.LayoutParams(itemWidth, itemWidth); 26 } 27 28 29 @Override 30 public View getView(final int position, View view, ViewGroup viewGroup) { 31 if (view == null) { 32 view = View.inflate(mContext, R.layout.item_photo_grid, null); 33 view.setLayoutParams(itemLayoutParams); 34 } 35 36 ImageView img_photo = ViewHolder.get(view, R.id.img_photo); 37 ImageModel imageModel = mDatas.get(position); 38 Glide.with(mContext).load(imageModel.getOriginalPath()) 39 .dontAnimate() 40 .centerCrop() 41 .override(300, 300) 42 .placeholder(com.zc.photoselector.R.drawable.ic_loading_white) 43 .error(com.zc.photoselector.R.drawable.mis_default_error) 44 .into(img_photo); 45 46 //查看大图 47 img_photo.setOnClickListener(new View.OnClickListener() { 48 @Override 49 public void onClick(View view) { 50 Intent intent = new Intent(mContext, PhotoPreviewActivity.class); 51 intent.putExtra("index", position); 52 intent.putExtra("images", (ArrayList) mDatas); 53 mContext.startActivity(intent); 54 } 55 }); 56 return view; 57 } 58 59 }
item_photo_grid 布局文件
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent"> 4 5 <ImageView 6 android:id="@+id/img_photo" 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent" 9 android:layout_gravity="center" 10 android:scaleType="centerCrop" /> 11 12 </RelativeLayout>
activity引用
1 gv_photo = (NoScrollGridView) findViewById(R.id.gv_photo); 2 list = new ArrayList<>(); 3 gv_photo.post(new Runnable() { 4 @Override 5 public void run() { 6 adapter = new PhotoGridAdapter(ServiceActivity.this, list, gv_photo.getWidth()); 7 gv_photo.setAdapter(adapter); 8 } 9 });