Permalink
Browse files

新增QQ风格带数字效果功能

  • Loading branch information...
1 parent 9e8096b commit 02777cc1d92465563709801062d1c05a253e18e5 @luck1991 luck1991 committed Jan 11, 2017
@@ -4,11 +4,13 @@
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;
@@ -42,15 +44,17 @@
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) {
+ 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) {
@@ -62,6 +66,7 @@ public void bindImagesData(List<LocalMedia> images) {
public void bindSelectImages(List<LocalMedia> images) {
this.selectImages = images;
notifyDataSetChanged();
+ subSelectPosition();
if (imageSelectChangedListener != null) {
imageSelectChangedListener.onChange(selectImages);
}
@@ -110,16 +115,21 @@ public void onClick(View v) {
} 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.setImageResource(cb_drawable);
+ contentHolder.check.setBackgroundResource(cb_drawable);
if (selectMode == Constants.MODE_SINGLE) {
- contentHolder.check.setVisibility(View.GONE);
+ contentHolder.ll_check.setVisibility(View.GONE);
} else {
- contentHolder.check.setVisibility(View.VISIBLE);
+ contentHolder.ll_check.setVisibility(View.VISIBLE);
}
+ if (is_checked_num) {
+ notifyCheckChanged(contentHolder, image);
+ }
+
+ selectImage(contentHolder, isSelected(image), false);
- selectImage(contentHolder, isSelected(image));
if (type == LocalMediaLoader.TYPE_VIDEO) {
Glide.with(context).load(path).into(contentHolder.picture);
long duration = image.getDuration();
@@ -137,9 +147,8 @@ public void onClick(View v) {
.into(contentHolder.picture);
contentHolder.rl_duration.setVisibility(View.GONE);
}
-
if (enablePreview || enablePreviewVideo) {
- contentHolder.check.setOnClickListener(new View.OnClickListener() {
+ contentHolder.ll_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeCheckboxState(contentHolder, image);
@@ -179,38 +188,57 @@ public HeaderViewHolder(View itemView) {
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView picture;
- ImageView check;
+ 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 = (ImageView) itemView.findViewById(R.id.check);
+ 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;
@@ -219,23 +247,43 @@ private void changeCheckboxState(ViewHolder contentHolder, LocalMedia image) {
for (LocalMedia media : selectImages) {
if (media.getPath().equals(image.getPath())) {
selectImages.remove(media);
+ subSelectPosition();
break;
}
}
} else {
selectImages.add(image);
+ image.setNum(selectImages.size());
}
- selectImage(contentHolder, !isChecked);
+ //通知点击项发生了改变
+ notifyItemChanged(contentHolder.getAdapterPosition());
+
+ selectImage(contentHolder, !isChecked, true);
if (imageSelectChangedListener != null) {
imageSelectChangedListener.onChange(selectImages);
}
}
- public void selectImage(ViewHolder holder, boolean isChecked) {
+ /**
+ * 更新选择的顺序
+ */
+ 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) {
- Animation animation = OptAnimationLoader.loadAnimation(context, R.anim.modal_in);
- holder.check.startAnimation(animation);
+ 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);
@@ -15,7 +15,10 @@
private long duration;
private long lastUpdateAt;
private boolean isChecked;
+ public int position;
+ private int num;
private int type;
+ private boolean isAnim;
public LocalMedia(String path, long lastUpdateAt, long duration, int type) {
this.path = path;
@@ -24,6 +27,22 @@ public LocalMedia(String path, long lastUpdateAt, long duration, int type) {
this.type = type;
}
+ public boolean isAnim() {
+ return isAnim;
+ }
+
+ public void setAnim(boolean anim) {
+ isAnim = anim;
+ }
+
+ public int getNum() {
+ return num;
+ }
+
+ public void setNum(int num) {
+ this.num = num;
+ }
+
public int getType() {
return type;
}
@@ -66,6 +66,7 @@ public static void startPhoto(Activity activity, Options options) {
intent.putExtra(Constants.EXTRA_CROP_H, options.getCropH());
intent.putExtra(Constants.EXTRA_VIDEO_SECOND, options.getRecordVideoSecond());
intent.putExtra(Constants.EXTRA_DEFINITION, options.getRecordVideoDefinition());
+ intent.putExtra(Constants.EXTRA_IS_CHECKED_NUM, options.isCheckNumMode());
activity.startActivityForResult(intent, Constants.REQUEST_IMAGE);
activity.overridePendingTransition(R.anim.slide_bottom_in, 0);
}
@@ -90,6 +91,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
cropH = getIntent().getIntExtra(Constants.EXTRA_CROP_H, 0);
recordVideoSecond = getIntent().getIntExtra(Constants.EXTRA_VIDEO_SECOND, 0);
definition = getIntent().getIntExtra(Constants.EXTRA_DEFINITION, Constants.HIGH);
+ is_checked_num = getIntent().getBooleanExtra(Constants.EXTRA_IS_CHECKED_NUM, false);
titleBar = (PublicTitleBar) findViewById(R.id.titleBar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
tv_empty = (TextView) findViewById(R.id.tv_empty);
@@ -248,6 +250,7 @@ private void startImageGridActivity(String folderName, List<LocalMedia> images)
intent.putExtra(Constants.EXTRA_MAX_SPAN_COUNT, spanCount);
intent.putExtra(Constants.EXTRA_VIDEO_SECOND, recordVideoSecond);
intent.putExtra(Constants.EXTRA_DEFINITION, definition);
+ intent.putExtra(Constants.EXTRA_IS_CHECKED_NUM, is_checked_num);
intent.setClass(mContext, ImageGridActivity.class);
startActivityForResult(intent, Constants.REQUEST_IMAGE);
}
@@ -50,6 +50,7 @@
protected int recordVideoSecond = 0;
protected int definition = 3;
protected boolean isCompress;
+ protected boolean is_checked_num;
protected Gson gson = new Gson();
@Override
@@ -12,6 +12,7 @@
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
@@ -100,6 +101,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
cropH = getIntent().getIntExtra(Constants.EXTRA_CROP_H, 0);
definition = getIntent().getIntExtra(Constants.EXTRA_DEFINITION, Constants.HIGH);
recordVideoSecond = getIntent().getIntExtra(Constants.EXTRA_VIDEO_SECOND, 0);
+ is_checked_num = getIntent().getBooleanExtra(Constants.EXTRA_IS_CHECKED_NUM, false);
if (savedInstanceState != null) {
cameraPath = savedInstanceState.getString(Constants.BUNDLE_CAMERA_PATH);
}
@@ -148,7 +150,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, ScreenUtils.dip2px(this, 2), false));
recyclerView.setLayoutManager(new GridLayoutManager(this, spanCount));
- adapter = new ImageGridAdapter(this, showCamera, maxSelectNum, selectMode, enablePreview, enablePreviewVideo, cb_drawable);
+ // 解决调用 notifyItemChanged 闪烁问题,取消默认动画
+ ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
+
+ // 如果是显示数据风格,则默认为qq选择风格
+ if (is_checked_num) {
+ tv_img_num.setBackgroundResource(R.drawable.message_oval_blue);
+ cb_drawable = R.drawable.checkbox_num_selector;
+ }
+ adapter = new ImageGridAdapter(this, showCamera, maxSelectNum, selectMode, enablePreview, enablePreviewVideo, cb_drawable, is_checked_num);
recyclerView.setAdapter(adapter);
if (selectImages.size() > 0) {
ChangeImageNumber(selectImages);
@@ -174,6 +184,7 @@ public void onClick(View view) {
intent.putExtra(Constants.EXTRA_MAX_SELECT_NUM, maxSelectNum);
intent.putExtra(Constants.BACKGROUND_COLOR, backgroundColor);
intent.putExtra(Constants.CHECKED_DRAWABLE, cb_drawable);
+ intent.putExtra(Constants.EXTRA_IS_CHECKED_NUM, is_checked_num);
intent.setClass(mContext, PreviewActivity.class);
startActivityForResult(intent, Constants.REQUEST_PREVIEW);
} else if (id == R.id.tv_ok) {
@@ -301,6 +312,7 @@ public void startPreview(List<LocalMedia> previewImages, int position) {
intent.putExtra(Constants.EXTRA_MAX_SELECT_NUM, maxSelectNum);
intent.putExtra(Constants.BACKGROUND_COLOR, backgroundColor);
intent.putExtra(Constants.CHECKED_DRAWABLE, cb_drawable);
+ intent.putExtra(Constants.EXTRA_IS_CHECKED_NUM, is_checked_num);
intent.setClass(mContext, PreviewActivity.class);
startActivityForResult(intent, Constants.REQUEST_PREVIEW);
}
@@ -508,23 +520,21 @@ protected void onSaveInstanceState(Bundle outState) {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
LocalMedia image = (LocalMedia) intent.getSerializableExtra("media");
+ List<LocalMedia> selectedImages = adapter.getSelectedImages();
if (action.equals(Constants.ACTION_ADD_PHOTO)) {
// 预览时新选择了图片
- List<LocalMedia> selectedImages = adapter.getSelectedImages();
selectedImages.add(image);
- adapter.bindSelectImages(selectedImages);
} else if (action.equals(Constants.ACTION_REMOVE_PHOTO)) {
// 预览时取消了之前选中的图片
- List<LocalMedia> selectImages = adapter.getSelectedImages();
- for (LocalMedia media : selectImages) {
+ for (LocalMedia media : selectedImages) {
if (media.getPath().equals(image.getPath())) {
- selectImages.remove(media);
- ChangeImageNumber(selectImages);
+ selectedImages.remove(media);
+ ChangeImageNumber(selectedImages);
break;
}
}
}
- adapter.notifyDataSetChanged();
+ adapter.bindSelectImages(selectedImages);
}
};
Oops, something went wrong.

0 comments on commit 02777cc

Please sign in to comment.