Skip to content

Commit

Permalink
Add Sample Image Edit Activity & Gallery Activity.
Browse files Browse the repository at this point in the history
  • Loading branch information
minetsh committed Jan 4, 2018
1 parent 071f932 commit f47a602
Show file tree
Hide file tree
Showing 24 changed files with 534 additions and 50 deletions.
128 changes: 117 additions & 11 deletions image/src/main/java/com/xingren/imaging/IMGGalleryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
Expand All @@ -16,11 +18,14 @@
import com.facebook.imagepipeline.common.RotationOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.xingren.imaging.gallery.IMGChooseMode;
import com.xingren.imaging.gallery.IMGScanTask;
import com.xingren.imaging.gallery.IMGScanner;
import com.xingren.imaging.model.IMGImageViewModel;
import com.xingren.imaging.gallery.model.IMGChooseMode;
import com.xingren.imaging.gallery.model.IMGImageInfo;
import com.xingren.imaging.gallery.model.IMGImageViewModel;
import com.xingren.imaging.widget.IMGGalleryHolderCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -38,6 +43,10 @@ public class IMGGalleryActivity extends Activity {

private Map<String, List<IMGImageViewModel>> mImages;

private List<IMGImageViewModel> mChooseImages = new ArrayList<>();

private static final String EXTRA_IMAGES = "IMAGES";

private static final String EXTRA_CHOOSE_MODE = "CHOOSE_MODE";

@Override
Expand All @@ -56,6 +65,21 @@ protected void onCreate(Bundle savedInstanceState) {
new IMGScanTask(this).execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.image_menu_gallery, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.image_menu_done) {
onDone();
return true;
}
return super.onOptionsItemSelected(item);
}

public void onImages(Map<String, List<IMGImageViewModel>> images) {
mImages = images;
if (images != null) {
Expand All @@ -69,12 +93,62 @@ public void onQuicklyImages(List<IMGImageViewModel> images) {
mAdapter.notifyDataSetChanged();
}

public static Intent buildIntent(Context context, IMGChooseMode mode) {
private void onImageCheckClick(int position) {
IMGImageViewModel item = mAdapter.getItem(position);
if (item != null) {
if (!item.isSelected()) {
if (mChooseImages.size() >= mGalleryMode.getMaxChooseCount()) {
// TODO 达到最大限度
mAdapter.notifyItemChanged(position, true);
return;
}
}

item.toggleSelected();
if (item.isSelected()) {
mChooseImages.add(item);
} else {
mChooseImages.remove(item);
}

mAdapter.notifyItemChanged(position, true);
}
}

private void onImageClick(int position) {
IMGImageViewModel item = mAdapter.getItem(position);
if (item != null) {
if (mGalleryMode.isSingleChoose()) {
mChooseImages.clear();
item.setSelected(true);
mChooseImages.add(item);
onDone();
}
}
}

private void onDone() {
ArrayList<IMGImageInfo> infos = new ArrayList<>();
for (IMGImageViewModel model : mChooseImages) {
infos.add(new IMGImageInfo(model));
}
setResult(RESULT_OK, new Intent().putParcelableArrayListExtra(EXTRA_IMAGES, infos));
finish();
}

public static ArrayList<IMGImageInfo> getImageInfos(Intent intent) {
if (intent != null) {
return intent.getParcelableArrayListExtra(EXTRA_IMAGES);
}
return null;
}

public static Intent newIntent(Context context, IMGChooseMode mode) {
return new Intent(context, IMGGalleryActivity.class)
.putExtra(EXTRA_CHOOSE_MODE, mode);
}

private class ImageAdapter extends RecyclerView.Adapter<ImageViewHolder> {
private class ImageAdapter extends RecyclerView.Adapter<ImageViewHolder> implements IMGGalleryHolderCallback {

private List<IMGImageViewModel> models;

Expand All @@ -84,8 +158,8 @@ private void setModels(List<IMGImageViewModel> models) {

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ImageViewHolder(getLayoutInflater()
.inflate(R.layout.image_layout_image, parent, false));
return new ImageViewHolder(getLayoutInflater().inflate(
R.layout.image_layout_image, parent, false), this);
}

@Override
Expand All @@ -97,29 +171,50 @@ public void onBindViewHolder(ImageViewHolder holder, int position) {
public int getItemCount() {
return models != null ? models.size() : 0;
}

private IMGImageViewModel getItem(int position) {
if (position >= 0 && position < getItemCount()) {
return models.get(position);
}
return null;
}

@Override
public void onViewHolderClick(RecyclerView.ViewHolder holder) {
onImageClick(holder.getAdapterPosition());
}

@Override
public void onCheckClick(RecyclerView.ViewHolder holder) {
onImageCheckClick(holder.getAdapterPosition());
}
}

private static class ImageViewHolder extends RecyclerView.ViewHolder {
private static class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private CheckBox mCheckBox;

private SimpleDraweeView mImageView;

private ImageViewHolder(View itemView) {
private IMGGalleryHolderCallback mCallback;

private ImageViewHolder(View itemView, IMGGalleryHolderCallback callback) {
super(itemView);
mCallback = callback;

mCheckBox = itemView.findViewById(R.id.cb_box);
mImageView = itemView.findViewById(R.id.sdv_image);

mCheckBox.setOnClickListener(this);
itemView.setOnClickListener(this);
}

private void update(IMGImageViewModel model, IMGChooseMode mode) {
mCheckBox.setChecked(model.isSelected());
mImageView.setImageURI(model.getUri());

mCheckBox.setVisibility(mode.isSingleChoose() ? View.GONE : View.VISIBLE);

ImageRequest request = ImageRequestBuilder.newBuilderWithSource(model.getUri())
.setLocalThumbnailPreviewsEnabled(true)
.disableDiskCache()
.setResizeOptions(new ResizeOptions(300, 300))
.setRotationOptions(RotationOptions.autoRotate())
.build();
Expand All @@ -131,5 +226,16 @@ private void update(IMGImageViewModel model, IMGChooseMode mode) {

mImageView.setController(controller);
}

@Override
public void onClick(View v) {
if (mCallback != null) {
if (v.getId() == R.id.cb_box) {
mCallback.onCheckClick(this);
} else {
mCallback.onViewHolderClick(this);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.os.AsyncTask;

import com.xingren.imaging.IMGGalleryActivity;
import com.xingren.imaging.model.IMGImageViewModel;
import com.xingren.imaging.gallery.model.IMGImageViewModel;

import java.lang.ref.WeakReference;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import android.provider.MediaStore;
import android.text.TextUtils;

import com.xingren.imaging.model.IMGImageViewModel;
import com.xingren.imaging.gallery.model.IMGImageViewModel;

import java.io.File;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xingren.imaging.gallery;
package com.xingren.imaging.gallery.model;

import android.os.Parcel;
import android.os.Parcelable;
Expand Down Expand Up @@ -89,11 +89,17 @@ public Builder setOriginalChangeable(boolean originalChangeable) {

public Builder setSingleChoose(boolean single) {
mode.isSingleChoose = single;
if (single) {
mode.maxChooseCount = 1;
}
return this;
}

public Builder setMaxChooseCount(int maxChooseCount) {
mode.maxChooseCount = maxChooseCount;
if (mode.isSingleChoose) {
mode.maxChooseCount = Math.min(1, maxChooseCount);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xingren.imaging.model;
package com.xingren.imaging.gallery.model;

import android.net.Uri;
import android.os.Parcel;
Expand All @@ -10,7 +10,7 @@

public class IMGImageInfo implements Parcelable {

private int size;
private long size;

private int width;

Expand All @@ -23,14 +23,23 @@ public class IMGImageInfo implements Parcelable {
private Uri uri;

protected IMGImageInfo(Parcel in) {
size = in.readInt();
size = in.readLong();
width = in.readInt();
height = in.readInt();
isOriginal = in.readByte() != 0;
isEdited = in.readByte() != 0;
uri = in.readParcelable(Uri.class.getClassLoader());
}

public IMGImageInfo(IMGImageViewModel model) {
this.size = model.getSize();
this.width = model.getWidth();
this.height = model.getHeight();
this.isOriginal = model.isOriginal();
this.isEdited = false;
this.uri = model.getUri();
}

public static final Creator<IMGImageInfo> CREATOR = new Creator<IMGImageInfo>() {
@Override
public IMGImageInfo createFromParcel(Parcel in) {
Expand All @@ -43,11 +52,11 @@ public IMGImageInfo[] newArray(int size) {
}
};

public int getSize() {
public long getSize() {
return size;
}

public void setSize(int size) {
public void setSize(long size) {
this.size = size;
}

Expand Down Expand Up @@ -98,7 +107,7 @@ public int describeContents() {

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(size);
dest.writeLong(size);
dest.writeInt(width);
dest.writeInt(height);
dest.writeByte((byte) (isOriginal ? 1 : 0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xingren.imaging.model;
package com.xingren.imaging.gallery.model;

import android.net.Uri;

Expand Down Expand Up @@ -48,6 +48,10 @@ public void setSelected(boolean selected) {
isSelected = selected;
}

public void toggleSelected() {
isSelected = !isSelected;
}

public int getWidth() {
return width;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.xingren.imaging.widget;

import android.support.v7.widget.RecyclerView;

/**
* Created by felix on 2018/1/4 下午3:53.
*/

public interface IMGGalleryHolderCallback extends IMGViewHolderCallback {

void onCheckClick(RecyclerView.ViewHolder holder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.xingren.imaging.widget;

import android.support.v7.widget.RecyclerView;

/**
* Created by felix on 2018/1/4 下午3:52.
*/

public interface IMGViewHolderCallback {

void onViewHolderClick(RecyclerView.ViewHolder holder);
}
3 changes: 2 additions & 1 deletion image/src/main/res/layout/image_gallery_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/image_color_primary">
android:background="@color/image_color_primary"
android:padding="6dp">

<TextView
android:layout_width="wrap_content"
Expand Down
9 changes: 9 additions & 0 deletions image/src/main/res/menu/image_menu_gallery.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/image_menu_done"
android:showAsAction="always"
android:title="@string/image_done" />

</menu>
6 changes: 6 additions & 0 deletions image/src/main/res/values/image_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
<string name="image_done">完成</string>
<string name="image_cancel">取消</string>
<string name="image_rotate">旋转</string>

<string name="image_mosaic_tip">请直接在图片上涂抹虚化</string>
<string name="image_undo">撤销</string>
<string name="image_all_photo">所有照片</string>
<string name="image_original">原图</string>
<string name="image_preview">预览</string>
</resources>
8 changes: 0 additions & 8 deletions image/src/main/res/values/strings.xml

This file was deleted.

Loading

0 comments on commit f47a602

Please sign in to comment.