-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ImageGridAdapter.java
328 lines (292 loc) · 11.6 KB
/
ImageGridAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package com.yalantis.ucrop.adapter;
import android.content.Context;
import android.graphics.PorterDuff;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yalantis.ucrop.R;
import com.yalantis.ucrop.dialog.OptAnimationLoader;
import com.yalantis.ucrop.entity.LocalMedia;
import com.yalantis.ucrop.ui.ImageGridActivity;
import com.yalantis.ucrop.util.Constants;
import com.yalantis.ucrop.util.LocalMediaLoader;
import java.util.ArrayList;
import java.util.List;
/**
* Created by dee on 15/11/19.
*/
public class ImageGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int TYPE_CAMERA = 1;
public static final int TYPE_PICTURE = 2;
private Context context;
private boolean showCamera = true;
private OnPhotoSelectChangedListener imageSelectChangedListener;
private int maxSelectNum;
private List<LocalMedia> images = new ArrayList<LocalMedia>();
private List<LocalMedia> selectImages = new ArrayList<LocalMedia>();
private boolean enablePreview;
private int selectMode = Constants.MODE_MULTIPLE;
private boolean enablePreviewVideo = false;
private int cb_drawable;
private boolean is_checked_num;
public ImageGridAdapter(Context context, boolean showCamera, int maxSelectNum, int mode, boolean enablePreview, boolean enablePreviewVideo, int cb_drawable, boolean is_checked_num) {
this.context = context;
this.selectMode = mode;
this.showCamera = showCamera;
this.maxSelectNum = maxSelectNum;
this.enablePreview = enablePreview;
this.enablePreviewVideo = enablePreviewVideo;
this.cb_drawable = cb_drawable;
this.is_checked_num = is_checked_num;
}
public void bindImagesData(List<LocalMedia> images) {
this.images = images;
notifyDataSetChanged();
}
public void bindSelectImages(List<LocalMedia> images) {
this.selectImages = images;
notifyDataSetChanged();
subSelectPosition();
if (imageSelectChangedListener != null) {
imageSelectChangedListener.onChange(selectImages);
}
}
public List<LocalMedia> getSelectedImages() {
return selectImages;
}
public List<LocalMedia> getImages() {
return images;
}
@Override
public int getItemViewType(int position) {
if (showCamera && position == 0) {
return TYPE_CAMERA;
} else {
return TYPE_PICTURE;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_CAMERA) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_camera, parent, false);
return new HeaderViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_grid_item, parent, false);
return new ViewHolder(view);
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (getItemViewType(position) == TYPE_CAMERA) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imageSelectChangedListener != null) {
imageSelectChangedListener.onTakePhoto();
}
}
});
} else {
final ViewHolder contentHolder = (ViewHolder) holder;
final LocalMedia image = images.get(showCamera ? position - 1 : position);
image.position = contentHolder.getAdapterPosition();
String path = image.getPath();
final int type = image.getType();
contentHolder.check.setBackgroundResource(cb_drawable);
if (selectMode == Constants.MODE_SINGLE) {
contentHolder.ll_check.setVisibility(View.GONE);
} else {
contentHolder.ll_check.setVisibility(View.VISIBLE);
}
if (is_checked_num) {
notifyCheckChanged(contentHolder, image);
}
selectImage(contentHolder, isSelected(image), false);
if (type == LocalMediaLoader.TYPE_VIDEO) {
Glide.with(context).load(path).into(contentHolder.picture);
long duration = image.getDuration();
contentHolder.rl_duration.setVisibility(View.VISIBLE);
contentHolder.tv_duration.setText("时长:" + timeParse(duration));
} else {
Glide.with(context)
.load(path)
.centerCrop()
.thumbnail(0.5f)
.placeholder(R.drawable.image_placeholder)
.error(R.drawable.image_placeholder)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.dontAnimate()
.into(contentHolder.picture);
contentHolder.rl_duration.setVisibility(View.GONE);
}
if (enablePreview || enablePreviewVideo) {
contentHolder.ll_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeCheckboxState(contentHolder, image);
}
});
}
contentHolder.contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (type == LocalMediaLoader.TYPE_VIDEO && (selectMode == Constants.MODE_SINGLE || enablePreviewVideo) && imageSelectChangedListener != null) {
int index = showCamera ? position - 1 : position;
imageSelectChangedListener.onPictureClick(image, index);
} else if (type == LocalMediaLoader.TYPE_IMAGE && (selectMode == Constants.MODE_SINGLE || enablePreview) && imageSelectChangedListener != null) {
int index = showCamera ? position - 1 : position;
imageSelectChangedListener.onPictureClick(image, index);
} else {
changeCheckboxState(contentHolder, image);
}
}
});
}
}
@Override
public int getItemCount() {
return showCamera ? images.size() + 1 : images.size();
}
public class HeaderViewHolder extends RecyclerView.ViewHolder {
View headerView;
public HeaderViewHolder(View itemView) {
super(itemView);
headerView = itemView;
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView picture;
TextView check;
TextView tv_duration;
View contentView;
LinearLayout ll_check;
RelativeLayout rl_duration;
public ViewHolder(View itemView) {
super(itemView);
contentView = itemView;
picture = (ImageView) itemView.findViewById(R.id.picture);
check = (TextView) itemView.findViewById(R.id.check);
ll_check = (LinearLayout) itemView.findViewById(R.id.ll_check);
tv_duration = (TextView) itemView.findViewById(R.id.tv_duration);
rl_duration = (RelativeLayout) itemView.findViewById(R.id.rl_duration);
}
}
public boolean isSelected(LocalMedia image) {
image.setAnim(false);
for (LocalMedia media : selectImages) {
if (media.getPath().equals(image.getPath())) {
image.setAnim(true);
return true;
}
}
return false;
}
/**
* 选择按钮更新
*/
private void notifyCheckChanged(ViewHolder viewHolder, LocalMedia imageBean) {
viewHolder.check.setText("");
for (LocalMedia media : selectImages) {
if (media.getPath().equals(imageBean.getPath())) {
imageBean.setNum(media.getNum());
viewHolder.check.setText(String.valueOf(imageBean.getNum()));
}
}
}
/**
* 改变图片选中状态
*
* @param contentHolder
* @param image
*/
private void changeCheckboxState(ViewHolder contentHolder, LocalMedia image) {
boolean isChecked = contentHolder.check.isSelected();
if (selectImages.size() >= maxSelectNum && !isChecked) {
Toast.makeText(context, context.getString(R.string.message_max_num, maxSelectNum), Toast.LENGTH_LONG).show();
return;
}
if (isChecked) {
for (LocalMedia media : selectImages) {
if (media.getPath().equals(image.getPath())) {
selectImages.remove(media);
subSelectPosition();
break;
}
}
} else {
selectImages.add(image);
image.setNum(selectImages.size());
}
//通知点击项发生了改变
notifyItemChanged(contentHolder.getAdapterPosition());
selectImage(contentHolder, !isChecked, true);
if (imageSelectChangedListener != null) {
imageSelectChangedListener.onChange(selectImages);
}
}
/**
* 更新选择的顺序
*/
private void subSelectPosition() {
if (is_checked_num) {
for (int index = 0, len = selectImages.size(); index < len; index++) {
LocalMedia media = selectImages.get(index);
media.setNum(index + 1);
notifyItemChanged(media.position);
}
}
}
public void selectImage(ViewHolder holder, boolean isChecked, boolean isAnim) {
holder.check.setSelected(isChecked);
if (isChecked) {
if (isAnim) {
Animation animation = OptAnimationLoader.loadAnimation(context, R.anim.modal_in);
holder.check.startAnimation(animation);
}
holder.picture.setColorFilter(ContextCompat.getColor(context, R.color.image_overlay2), PorterDuff.Mode.SRC_ATOP);
} else {
holder.picture.setColorFilter(ContextCompat.getColor(context, R.color.image_overlay), PorterDuff.Mode.SRC_ATOP);
}
}
public interface OnPhotoSelectChangedListener {
void onTakePhoto();
void onChange(List<LocalMedia> selectImages);
void onPictureClick(LocalMedia media, int position);
}
public void setOnPhotoSelectChangedListener(OnPhotoSelectChangedListener imageSelectChangedListener) {
this.imageSelectChangedListener = imageSelectChangedListener;
}
/**
* 毫秒转时分秒
*
* @param duration
* @return
*/
public String timeParse(long duration) {
String time = "";
long minute = duration / 60000;
long seconds = duration % 60000;
long second = Math.round((float) seconds / 1000);
if (minute < 10) {
time += "0";
}
time += minute + ":";
if (second < 10) {
time += "0";
}
time += second;
return time;
}
}