Skip to content

Commit 06f7776

Browse files
committed
add engine
1 parent 5e848e8 commit 06f7776

File tree

15 files changed

+507
-71
lines changed

15 files changed

+507
-71
lines changed

app/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ android {
1717
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1818
}
1919
}
20+
repositories {
21+
flatDir {
22+
dirs 'libs' //this way we can find the .aar file in libs folder
23+
}
24+
}
2025
}
2126

2227
dependencies {
2328
compile fileTree(include: ['*.jar'], dir: 'libs')
2429
compile 'com.android.support:appcompat-v7:23.1.1'
2530
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
31+
compile 'com.github.bumptech.glide:glide:3.6.1'
32+
compile 'com.squareup.picasso:picasso:2.5.2'
33+
// compile(name: 'gallery-release', ext: 'aar')
2634
compile project(':gallery')
2735
}

app/libs/gallery-release.aar

82.5 KB
Binary file not shown.

app/src/main/java/cc/fotoplace/fotoplacegallery/MainActivity.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
import android.util.Log;
88
import android.view.View;
99

10+
import com.nostra13.universalimageloader.core.ImageLoader;
11+
1012
import java.util.List;
1113

1214
import io.valuesfeng.picker.Picker;
15+
import io.valuesfeng.picker.engine.glide.GlideEngine;
16+
import io.valuesfeng.picker.engine.imageloader.ImageLoaderEngine;
1317
import io.valuesfeng.picker.utils.PicturePickerUtils;
1418

1519
public class MainActivity extends FragmentActivity {
@@ -34,6 +38,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3438
}
3539

3640
public void onClickButton(View view) {
37-
Picker.from(this).count(0,3).setEnableCamera(true).singleChoice().forResult(1);
41+
Picker.from(this).count(0,3)
42+
.setEnableCamera(true)
43+
.setEngine(new GlideEngine(R.mipmap.ic_launcher,R.mipmap.ic_launcher))
44+
.forResult(1);
3845
}
3946
}

gallery/src/main/java/io/valuesfeng/picker/ImageSelectActivity.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
import android.widget.TextView;
2121
import android.widget.Toast;
2222

23-
import com.nostra13.universalimageloader.core.ImageLoader;
24-
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;
25-
2623
import java.util.ArrayList;
2724

2825
import io.valuesfeng.picker.control.PictureCollection;
@@ -39,6 +36,7 @@ public class ImageSelectActivity extends FragmentActivity implements Confirmatio
3936
public static final String EXTRA_RESULT_SELECTION = BundleUtils.buildKey(ImageSelectActivity.class, "EXTRA_RESULT_SELECTION");
4037
public static final String EXTRA_SELECTION_SPEC = BundleUtils.buildKey(ImageSelectActivity.class, "EXTRA_SELECTION_SPEC");
4138
public static final String EXTRA_RESUME_LIST = BundleUtils.buildKey(ImageSelectActivity.class, "EXTRA_RESUME_LIST");
39+
// public static final String EXTRA_ENGINE = BundleUtils.buildKey(ImageSelectActivity.class, "EXTRA_ENGINE");
4240

4341
public static final String STATE_CAPTURE_PHOTO_URI = BundleUtils.buildKey(ImageSelectActivity.class, "STATE_CAPTURE_PHOTO_URI");
4442

@@ -76,7 +74,6 @@ public void onChange(int maxCount, int selectCount) {
7674
});
7775

7876
mGridView = (GridView) findViewById(R.id.gridView);
79-
mGridView.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(),false,true));
8077

8178
mListView = (ListView) findViewById(R.id.listView);
8279
btnBack = (ImageView) findViewById(R.id.btn_back);

gallery/src/main/java/io/valuesfeng/picker/Picker.java

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,65 +22,71 @@
2222
import android.support.v4.app.Fragment;
2323

2424

25+
import com.bumptech.glide.Glide;
26+
import com.squareup.picasso.Picasso;
27+
28+
import java.io.Serializable;
2529
import java.lang.ref.WeakReference;
2630
import java.util.ArrayList;
2731
import java.util.List;
2832
import java.util.Set;
2933

34+
import io.valuesfeng.picker.engine.LoadEngine;
3035
import io.valuesfeng.picker.model.SelectionSpec;
3136

3237
/**
3338
*/
3439
public final class Picker {
40+
private static final String INITIALIZE_PICKER_ERROR = "Try to initialize Picker which had already been initialized before";
41+
private static boolean hasInitPicker;
3542
private final WeakReference<Activity> mContext;
3643
private final WeakReference<Fragment> mFragment;
37-
public static final String TAG = Picker.class.getSimpleName();
3844
private final Set<MimeType> mMimeType;
3945
private final SelectionSpec mSelectionSpec;
40-
private int mActivityOrientation;
46+
private LoadEngine engine; //图片加载器 glide imageloder picasso
4147
private List<Uri> mResumeList;
4248

43-
/**
44-
*/
45-
Picker(Activity context, Set<MimeType> mimeType) {
46-
mContext = new WeakReference<>(context);
47-
mFragment = null;
48-
mMimeType = mimeType;
49-
mSelectionSpec = new SelectionSpec();
50-
mResumeList = new ArrayList<>();
51-
mActivityOrientation = -1;
52-
}
53-
54-
Picker(Activity context) {
55-
mContext = new WeakReference<>(context);
56-
mFragment = null;
57-
mMimeType = MimeType.allOf();
58-
mSelectionSpec = new SelectionSpec();
59-
mResumeList = new ArrayList<>();
60-
mActivityOrientation = -1;
61-
}
62-
63-
/**
64-
*/
6549
Picker(Activity activity, Fragment fragment, Set<MimeType> mimeType) {
6650
mContext = new WeakReference<>(activity);
67-
mFragment = new WeakReference<>(fragment);
51+
if (fragment != null)
52+
mFragment = new WeakReference<>(fragment);
53+
else
54+
mFragment = null;
6855
mMimeType = mimeType;
6956
mSelectionSpec = new SelectionSpec();
7057
mResumeList = new ArrayList<>();
71-
mActivityOrientation = -1;
7258
}
7359

7460
Picker(Activity activity, Fragment fragment) {
7561
mContext = new WeakReference<>(activity);
76-
mFragment = new WeakReference<>(fragment);
62+
if (fragment != null)
63+
mFragment = new WeakReference<>(fragment);
64+
else
65+
mFragment = null;
7766
mMimeType = MimeType.allOf();
7867
mSelectionSpec = new SelectionSpec();
7968
mResumeList = new ArrayList<>();
80-
mActivityOrientation = -1;
8169
}
8270

83-
public Picker setEnableCamera(boolean mEnableCamera){
71+
72+
/**
73+
* set iamge load engine
74+
*
75+
* @param engine
76+
* @return
77+
*/
78+
public Picker setEngine(LoadEngine engine) {
79+
this.engine = engine;
80+
return this;
81+
}
82+
83+
/**
84+
* set the first item open camera
85+
*
86+
* @param mEnableCamera
87+
* @return
88+
*/
89+
public Picker setEnableCamera(boolean mEnableCamera) {
8490
mSelectionSpec.setmEnableCamera(mEnableCamera);
8591
return this;
8692
}
@@ -136,13 +142,18 @@ public Picker resume(List<Uri> uriList) {
136142
* @param requestCode identity of the requester activity.
137143
*/
138144
public void forResult(int requestCode) {
145+
if (engine == null)
146+
throw new ExceptionInInitializerError(LoadEngine.INITIALIZE_ENGINE_ERROR);
147+
139148
Activity activity = getActivity();
140149
if (activity == null) {
141150
return; // cannot continue;
142151
}
143152
mSelectionSpec.setMimeTypeSet(mMimeType);
153+
mSelectionSpec.setEngine(engine);
144154
Intent intent = new Intent(activity, ImageSelectActivity.class);
145155
intent.putExtra(ImageSelectActivity.EXTRA_SELECTION_SPEC, mSelectionSpec);
156+
// intent.putExtra(ImageSelectActivity.EXTRA_ENGINE, (Serializable) engine);
146157
intent.putParcelableArrayListExtra(ImageSelectActivity.EXTRA_RESUME_LIST, (ArrayList<? extends android.os.Parcelable>) mResumeList);
147158

148159
Fragment fragment = getFragment();
@@ -151,21 +162,34 @@ public void forResult(int requestCode) {
151162
} else {
152163
activity.startActivityForResult(intent, requestCode);
153164
}
165+
hasInitPicker = false;
154166
}
155167

156168
public static Picker from(Activity activity) {
157-
return new Picker(activity);
169+
if (hasInitPicker)
170+
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
171+
hasInitPicker = true;
172+
return new Picker(activity, null);
158173
}
159174

160-
public static Picker from(Activity activity, Set<MimeType> mimeType){
161-
return new Picker(activity, mimeType);
175+
public static Picker from(Activity activity, Set<MimeType> mimeType) {
176+
if (hasInitPicker)
177+
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
178+
hasInitPicker = true;
179+
return new Picker(activity, null, mimeType);
162180
}
163181

164-
public static Picker from(Fragment fragment) throws InterruptedException{
182+
public static Picker from(Fragment fragment) {
183+
if (hasInitPicker)
184+
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
185+
hasInitPicker = true;
165186
return new Picker(fragment.getActivity(), fragment);
166187
}
167188

168-
public static Picker from(Fragment fragment, Set<MimeType> mimeType) throws InterruptedException{
189+
public static Picker from(Fragment fragment, Set<MimeType> mimeType) {
190+
if (hasInitPicker)
191+
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
192+
hasInitPicker = true;
169193
return new Picker(fragment.getActivity(), fragment, mimeType);
170194
}
171195

gallery/src/main/java/io/valuesfeng/picker/control/PictureCollection.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
4949
if (context == null) {
5050
return null;
5151
}
52-
5352
Album album = args.getParcelable(ARGS_ALBUM);
5453
if (album == null) {
5554
return null;
5655
}
57-
5856
return PictureLoader.newInstance(context, album, selectionSpec);
5957
}
6058

gallery/src/main/java/io/valuesfeng/picker/control/SelectedUriCollection.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Set;
2828

29+
import io.valuesfeng.picker.engine.LoadEngine;
2930
import io.valuesfeng.picker.model.SelectionSpec;
3031
import io.valuesfeng.picker.utils.BundleUtils;
3132

@@ -108,6 +109,10 @@ public boolean isSingleChoose() {
108109
return mSpec.isSingleChoose();
109110
}
110111

112+
public LoadEngine getEngine(){
113+
return mSpec.getEngine();
114+
}
115+
111116
public void setOnSelectionChange(OnSelectionChange onSelectionChange) {
112117
this.onSelectionChange = onSelectionChange;
113118
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.valuesfeng.picker.engine;
2+
3+
import android.os.Parcelable;
4+
import android.widget.GridView;
5+
import android.widget.ImageView;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* Author: valuesfeng
11+
* Version V1.0
12+
* Date: 16/1/1
13+
* Description:
14+
* Modification History:
15+
* Date Author Version Description
16+
* -----------------------------------------------------------------------------------
17+
* 16/1/1 valuesfeng 1.0 1.0
18+
* Why & What is modified:
19+
*/
20+
public interface LoadEngine extends Parcelable{
21+
String INITIALIZE_ENGINE_ERROR = "initialize error,image load engine can not be null";
22+
23+
void displayImage(int res, ImageView imageView);
24+
void displayImage(String path, ImageView imageView);
25+
void pauseOnScroll(GridView view);
26+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.valuesfeng.picker.engine.glide;
2+
3+
import android.content.Context;
4+
import android.os.Parcel;
5+
import android.widget.GridView;
6+
import android.widget.ImageView;
7+
8+
import com.bumptech.glide.Glide;
9+
10+
import io.valuesfeng.picker.R;
11+
import io.valuesfeng.picker.engine.LoadEngine;
12+
13+
/**
14+
* Author: valuesfeng
15+
* Version V1.0
16+
* Date: 16/1/1
17+
* Description:
18+
* Modification History:
19+
* Date Author Version Description
20+
* -----------------------------------------------------------------------------------
21+
* 16/1/1 valuesfeng 1.0 1.0
22+
* Why & What is modified:
23+
*/
24+
public class GlideEngine implements LoadEngine {
25+
26+
private int img_loading;
27+
private int camera_loading;
28+
29+
public GlideEngine() {
30+
this(0, 0);
31+
}
32+
33+
public GlideEngine(int camera_loading, int img_loading) {
34+
if (img_loading == 0)
35+
this.img_loading = R.drawable.image_not_exist;
36+
else
37+
this.img_loading = img_loading;
38+
if (camera_loading == 0)
39+
this.camera_loading = R.drawable.ic_camera;
40+
else
41+
this.camera_loading = camera_loading;
42+
}
43+
44+
@Override
45+
public void displayImage(String path, ImageView imageView) {
46+
chargeInit(imageView.getContext());
47+
Glide.with(imageView.getContext())
48+
.load(path)
49+
.centerCrop()
50+
.error(img_loading)
51+
.placeholder(img_loading)
52+
.crossFade()
53+
.into(imageView);
54+
}
55+
56+
@Override
57+
public void displayImage(int res, ImageView imageView) {
58+
chargeInit(imageView.getContext());
59+
Glide.with(imageView.getContext())
60+
.load(res)
61+
.centerCrop()
62+
.error(camera_loading)
63+
.placeholder(camera_loading)
64+
.crossFade()
65+
.into(imageView);
66+
}
67+
68+
private void chargeInit(Context context){
69+
if (Glide.get(context) == null) {
70+
throw new ExceptionInInitializerError(INITIALIZE_ENGINE_ERROR);
71+
}
72+
}
73+
@Override
74+
public void pauseOnScroll(GridView view) {
75+
76+
}
77+
78+
@Override
79+
public int describeContents() {
80+
return 0;
81+
}
82+
83+
@Override
84+
public void writeToParcel(Parcel dest, int flags) {
85+
dest.writeInt(this.img_loading);
86+
dest.writeInt(this.camera_loading);
87+
}
88+
89+
protected GlideEngine(Parcel in) {
90+
this.img_loading = in.readInt();
91+
this.camera_loading = in.readInt();
92+
}
93+
94+
public static final Creator<GlideEngine> CREATOR = new Creator<GlideEngine>() {
95+
public GlideEngine createFromParcel(Parcel source) {
96+
return new GlideEngine(source);
97+
}
98+
99+
public GlideEngine[] newArray(int size) {
100+
return new GlideEngine[size];
101+
}
102+
};
103+
}

0 commit comments

Comments
 (0)