Skip to content

Commit

Permalink
add engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuesFeng committed Jan 1, 2016
1 parent 5e848e8 commit 06f7776
Show file tree
Hide file tree
Showing 15 changed files with 507 additions and 71 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.squareup.picasso:picasso:2.5.2'
// compile(name: 'gallery-release', ext: 'aar')
compile project(':gallery')
}
Binary file added app/libs/gallery-release.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import android.util.Log;
import android.view.View;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import io.valuesfeng.picker.Picker;
import io.valuesfeng.picker.engine.glide.GlideEngine;
import io.valuesfeng.picker.engine.imageloader.ImageLoaderEngine;
import io.valuesfeng.picker.utils.PicturePickerUtils;

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

public void onClickButton(View view) {
Picker.from(this).count(0,3).setEnableCamera(true).singleChoice().forResult(1);
Picker.from(this).count(0,3)
.setEnableCamera(true)
.setEngine(new GlideEngine(R.mipmap.ic_launcher,R.mipmap.ic_launcher))
.forResult(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import java.util.ArrayList;

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

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

Expand Down Expand Up @@ -76,7 +74,6 @@ public void onChange(int maxCount, int selectCount) {
});

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

mListView = (ListView) findViewById(R.id.listView);
btnBack = (ImageView) findViewById(R.id.btn_back);
Expand Down
92 changes: 58 additions & 34 deletions gallery/src/main/java/io/valuesfeng/picker/Picker.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,71 @@
import android.support.v4.app.Fragment;


import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import io.valuesfeng.picker.engine.LoadEngine;
import io.valuesfeng.picker.model.SelectionSpec;

/**
*/
public final class Picker {
private static final String INITIALIZE_PICKER_ERROR = "Try to initialize Picker which had already been initialized before";
private static boolean hasInitPicker;
private final WeakReference<Activity> mContext;
private final WeakReference<Fragment> mFragment;
public static final String TAG = Picker.class.getSimpleName();
private final Set<MimeType> mMimeType;
private final SelectionSpec mSelectionSpec;
private int mActivityOrientation;
private LoadEngine engine; //图片加载器 glide imageloder picasso
private List<Uri> mResumeList;

/**
*/
Picker(Activity context, Set<MimeType> mimeType) {
mContext = new WeakReference<>(context);
mFragment = null;
mMimeType = mimeType;
mSelectionSpec = new SelectionSpec();
mResumeList = new ArrayList<>();
mActivityOrientation = -1;
}

Picker(Activity context) {
mContext = new WeakReference<>(context);
mFragment = null;
mMimeType = MimeType.allOf();
mSelectionSpec = new SelectionSpec();
mResumeList = new ArrayList<>();
mActivityOrientation = -1;
}

/**
*/
Picker(Activity activity, Fragment fragment, Set<MimeType> mimeType) {
mContext = new WeakReference<>(activity);
mFragment = new WeakReference<>(fragment);
if (fragment != null)
mFragment = new WeakReference<>(fragment);
else
mFragment = null;
mMimeType = mimeType;
mSelectionSpec = new SelectionSpec();
mResumeList = new ArrayList<>();
mActivityOrientation = -1;
}

Picker(Activity activity, Fragment fragment) {
mContext = new WeakReference<>(activity);
mFragment = new WeakReference<>(fragment);
if (fragment != null)
mFragment = new WeakReference<>(fragment);
else
mFragment = null;
mMimeType = MimeType.allOf();
mSelectionSpec = new SelectionSpec();
mResumeList = new ArrayList<>();
mActivityOrientation = -1;
}

public Picker setEnableCamera(boolean mEnableCamera){

/**
* set iamge load engine
*
* @param engine
* @return
*/
public Picker setEngine(LoadEngine engine) {
this.engine = engine;
return this;
}

/**
* set the first item open camera
*
* @param mEnableCamera
* @return
*/
public Picker setEnableCamera(boolean mEnableCamera) {
mSelectionSpec.setmEnableCamera(mEnableCamera);
return this;
}
Expand Down Expand Up @@ -136,13 +142,18 @@ public Picker resume(List<Uri> uriList) {
* @param requestCode identity of the requester activity.
*/
public void forResult(int requestCode) {
if (engine == null)
throw new ExceptionInInitializerError(LoadEngine.INITIALIZE_ENGINE_ERROR);

Activity activity = getActivity();
if (activity == null) {
return; // cannot continue;
}
mSelectionSpec.setMimeTypeSet(mMimeType);
mSelectionSpec.setEngine(engine);
Intent intent = new Intent(activity, ImageSelectActivity.class);
intent.putExtra(ImageSelectActivity.EXTRA_SELECTION_SPEC, mSelectionSpec);
// intent.putExtra(ImageSelectActivity.EXTRA_ENGINE, (Serializable) engine);
intent.putParcelableArrayListExtra(ImageSelectActivity.EXTRA_RESUME_LIST, (ArrayList<? extends android.os.Parcelable>) mResumeList);

Fragment fragment = getFragment();
Expand All @@ -151,21 +162,34 @@ public void forResult(int requestCode) {
} else {
activity.startActivityForResult(intent, requestCode);
}
hasInitPicker = false;
}

public static Picker from(Activity activity) {
return new Picker(activity);
if (hasInitPicker)
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
hasInitPicker = true;
return new Picker(activity, null);
}

public static Picker from(Activity activity, Set<MimeType> mimeType){
return new Picker(activity, mimeType);
public static Picker from(Activity activity, Set<MimeType> mimeType) {
if (hasInitPicker)
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
hasInitPicker = true;
return new Picker(activity, null, mimeType);
}

public static Picker from(Fragment fragment) throws InterruptedException{
public static Picker from(Fragment fragment) {
if (hasInitPicker)
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
hasInitPicker = true;
return new Picker(fragment.getActivity(), fragment);
}

public static Picker from(Fragment fragment, Set<MimeType> mimeType) throws InterruptedException{
public static Picker from(Fragment fragment, Set<MimeType> mimeType) {
if (hasInitPicker)
throw new ExceptionInInitializerError(INITIALIZE_PICKER_ERROR);
hasInitPicker = true;
return new Picker(fragment.getActivity(), fragment, mimeType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (context == null) {
return null;
}

Album album = args.getParcelable(ARGS_ALBUM);
if (album == null) {
return null;
}

return PictureLoader.newInstance(context, album, selectionSpec);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Set;

import io.valuesfeng.picker.engine.LoadEngine;
import io.valuesfeng.picker.model.SelectionSpec;
import io.valuesfeng.picker.utils.BundleUtils;

Expand Down Expand Up @@ -108,6 +109,10 @@ public boolean isSingleChoose() {
return mSpec.isSingleChoose();
}

public LoadEngine getEngine(){
return mSpec.getEngine();
}

public void setOnSelectionChange(OnSelectionChange onSelectionChange) {
this.onSelectionChange = onSelectionChange;
}
Expand Down
26 changes: 26 additions & 0 deletions gallery/src/main/java/io/valuesfeng/picker/engine/LoadEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.valuesfeng.picker.engine;

import android.os.Parcelable;
import android.widget.GridView;
import android.widget.ImageView;

import java.io.Serializable;

/**
* Author: valuesfeng
* Version V1.0
* Date: 16/1/1
* Description:
* Modification History:
* Date Author Version Description
* -----------------------------------------------------------------------------------
* 16/1/1 valuesfeng 1.0 1.0
* Why & What is modified:
*/
public interface LoadEngine extends Parcelable{
String INITIALIZE_ENGINE_ERROR = "initialize error,image load engine can not be null";

void displayImage(int res, ImageView imageView);
void displayImage(String path, ImageView imageView);
void pauseOnScroll(GridView view);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.valuesfeng.picker.engine.glide;

import android.content.Context;
import android.os.Parcel;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import io.valuesfeng.picker.R;
import io.valuesfeng.picker.engine.LoadEngine;

/**
* Author: valuesfeng
* Version V1.0
* Date: 16/1/1
* Description:
* Modification History:
* Date Author Version Description
* -----------------------------------------------------------------------------------
* 16/1/1 valuesfeng 1.0 1.0
* Why & What is modified:
*/
public class GlideEngine implements LoadEngine {

private int img_loading;
private int camera_loading;

public GlideEngine() {
this(0, 0);
}

public GlideEngine(int camera_loading, int img_loading) {
if (img_loading == 0)
this.img_loading = R.drawable.image_not_exist;
else
this.img_loading = img_loading;
if (camera_loading == 0)
this.camera_loading = R.drawable.ic_camera;
else
this.camera_loading = camera_loading;
}

@Override
public void displayImage(String path, ImageView imageView) {
chargeInit(imageView.getContext());
Glide.with(imageView.getContext())
.load(path)
.centerCrop()
.error(img_loading)
.placeholder(img_loading)
.crossFade()
.into(imageView);
}

@Override
public void displayImage(int res, ImageView imageView) {
chargeInit(imageView.getContext());
Glide.with(imageView.getContext())
.load(res)
.centerCrop()
.error(camera_loading)
.placeholder(camera_loading)
.crossFade()
.into(imageView);
}

private void chargeInit(Context context){
if (Glide.get(context) == null) {
throw new ExceptionInInitializerError(INITIALIZE_ENGINE_ERROR);
}
}
@Override
public void pauseOnScroll(GridView view) {

}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.img_loading);
dest.writeInt(this.camera_loading);
}

protected GlideEngine(Parcel in) {
this.img_loading = in.readInt();
this.camera_loading = in.readInt();
}

public static final Creator<GlideEngine> CREATOR = new Creator<GlideEngine>() {
public GlideEngine createFromParcel(Parcel source) {
return new GlideEngine(source);
}

public GlideEngine[] newArray(int size) {
return new GlideEngine[size];
}
};
}
Loading

0 comments on commit 06f7776

Please sign in to comment.