-
Notifications
You must be signed in to change notification settings - Fork 97
/
SmartImageTask.java
58 lines (46 loc) · 1.4 KB
/
SmartImageTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.jit.lib;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
public class SmartImageTask implements Runnable {
private static final int BITMAP_READY = 0;
private boolean cancelled = false;
private OnCompleteHandler onCompleteHandler;
private SmartImage image;
private Context context;
public static class OnCompleteHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap)msg.obj;
onComplete(bitmap);
}
public void onComplete(Bitmap bitmap){};
}
public interface OnCompleteListener {
void onSuccess(Bitmap bitmap);
void onFail();
}
public SmartImageTask(Context context, SmartImage image) {
this.image = image;
this.context = context;
}
@Override
public void run() {
if(image != null) {
complete(image.getBitmap(context));
context = null;
}
}
public void setOnCompleteHandler(OnCompleteHandler handler){
this.onCompleteHandler = handler;
}
public void cancel() {
cancelled = true;
}
public void complete(Bitmap bitmap){
if(onCompleteHandler != null && !cancelled) {
onCompleteHandler.sendMessage(onCompleteHandler.obtainMessage(BITMAP_READY, bitmap));
}
}
}