自定义相册、九宫格显示图片
生活随笔
收集整理的這篇文章主要介紹了
自定义相册、九宫格显示图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一 自定義相冊
結合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 });?
轉載于:https://www.cnblogs.com/suiyilaile/p/9159451.html
總結
以上是生活随笔為你收集整理的自定义相册、九宫格显示图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python基础-网络基础知识和网络编程
- 下一篇: LeetCode(90):子集 II