Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步对Luban的压缩停止,实例线程空转结束 #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions example/src/main/java/top/zibin/luban/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ public void accept(@NonNull List<File> list) {
}));
}

private Luban mLuban;//Rx不在意了
private <T> void withLs(final List<T> photos) {
Luban.with(this)
final int[] i = {0};
mLuban = Luban.with(this)
.load(photos)
.ignoreBy(100)
.setTargetDir(getPath())
Expand All @@ -197,12 +199,19 @@ public String rename(String filePath) {
})
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() { }
public void onStart() {
if(i[0] ==0){
//这次压缩完了就不再压缩,释放资源
mLuban.setContinue(false);
}
}

@Override
public void onSuccess(File file) {
Log.i(TAG, file.getAbsolutePath());
Log.i(TAG, "compress img :"+i[0]);
showResult(originPhotos, file);
i[0]++;
}

@Override
Expand Down
14 changes: 12 additions & 2 deletions library/src/main/java/top/zibin/luban/Luban.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private static File getImageCacheDir(Context context, String cacheName) {
/**
* start asynchronous compress thread
*/
private boolean mIsContinue = true;
private void launch(final Context context) {
if (mStreamProviders == null || mStreamProviders.size() == 0 && mCompressListener != null) {
mCompressListener.onError(new NullPointerException("image file cannot be null"));
Expand All @@ -130,6 +131,9 @@ private void launch(final Context context) {
@Override
public void run() {
try {
if(!mIsContinue){//线程空转,资源释放
return;
}
mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_START));

File result = compress(context, path);
Expand All @@ -145,6 +149,10 @@ public void run() {
}
}

public void setContinue(boolean isContinue){
mIsContinue = isContinue;
}

/**
* start compress and return the file
*/
Expand Down Expand Up @@ -229,14 +237,15 @@ public static class Builder {
private OnCompressListener mCompressListener;
private CompressionPredicate mCompressionPredicate;
private List<InputStreamProvider> mStreamProviders;
private Luban mLuban;

Builder(Context context) {
this.context = context;
this.mStreamProviders = new ArrayList<>();
}

private Luban build() {
return new Luban(this);
return mLuban = new Luban(this);
}

public Builder load(InputStreamProvider inputStreamProvider) {
Expand Down Expand Up @@ -358,8 +367,9 @@ public Builder filter(CompressionPredicate compressionPredicate) {
/**
* begin compress image with asynchronous
*/
public void launch() {
public Luban launch() {
build().launch(context);
return mLuban;
}

public File get(final String path) throws IOException {
Expand Down