-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathRCTImageSequenceView.java
More file actions
168 lines (134 loc) · 5.37 KB
/
RCTImageSequenceView.java
File metadata and controls
168 lines (134 loc) · 5.37 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package dk.madslee.imageSequence;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.os.AsyncTask;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class RCTImageSequenceView extends ImageView {
private Integer framesPerSecond = 24;
private Boolean loop = true;
private ArrayList<AsyncTask> activeTasks;
private HashMap<Integer, Bitmap> bitmaps;
private RCTResourceDrawableIdHelper resourceDrawableIdHelper;
public RCTImageSequenceView(Context context) {
super(context);
resourceDrawableIdHelper = new RCTResourceDrawableIdHelper();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private final Integer index;
private final String uri;
private final Context context;
public DownloadImageTask(Integer index, String uri, Context context) {
this.index = index;
this.uri = uri;
this.context = context;
}
@Override
protected Bitmap doInBackground(String... params) {
if (this.uri.startsWith("http")) {
return this.loadBitmapByExternalURL(this.uri);
}
return this.loadBitmapByLocalResource(this.uri);
}
private Bitmap loadBitmapByLocalResource(String uri) {
return BitmapFactory.decodeResource(this.context.getResources(), resourceDrawableIdHelper.getResourceDrawableId(this.context, uri));
}
private Bitmap loadBitmapByExternalURL(String uri) {
Bitmap bitmap = null;
try {
InputStream in = new URL(uri).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (!isCancelled()) {
onTaskCompleted(this, index, bitmap);
}
}
}
private void onTaskCompleted(DownloadImageTask downloadImageTask, Integer index, Bitmap bitmap) {
if (index == 0) {
// first image should be displayed as soon as possible.
this.setImageBitmap(bitmap);
}
bitmaps.put(index, bitmap);
activeTasks.remove(downloadImageTask);
if (activeTasks.isEmpty()) {
setupAnimationDrawable();
}
}
public void setImages(ArrayList<String> uris) {
if (isLoading()) {
// cancel ongoing tasks (if still loading previous images)
for (int index = 0; index < activeTasks.size(); index++) {
activeTasks.get(index).cancel(true);
}
}
activeTasks = new ArrayList<>(uris.size());
bitmaps = new HashMap<>(uris.size());
ThreadPoolExecutor defaultExecutor = (ThreadPoolExecutor) AsyncTask.THREAD_POOL_EXECUTOR;
ThreadPoolExecutor executor = new ThreadPoolExecutor(
defaultExecutor.getCorePoolSize(),
defaultExecutor.getMaximumPoolSize(),
defaultExecutor.getKeepAliveTime(TimeUnit.MILLISECONDS),
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()
);
for (int index = 0; index < uris.size(); index++) {
DownloadImageTask task = new DownloadImageTask(index, uris.get(index), getContext());
activeTasks.add(task);
try {
task.executeOnExecutor(executor);
} catch (RejectedExecutionException e){
Log.e("react-native-image-sequence", "DownloadImageTask failed" + e.getMessage());
break;
}
}
}
public void setFramesPerSecond(Integer framesPerSecond) {
this.framesPerSecond = framesPerSecond;
// updating frames per second, results in building a new AnimationDrawable (because we cant alter frame duration)
if (isLoaded()) {
setupAnimationDrawable();
}
}
public void setLoop(Boolean loop) {
this.loop = loop;
// updating looping, results in building a new AnimationDrawable
if (isLoaded()) {
setupAnimationDrawable();
}
}
private boolean isLoaded() {
return !isLoading() && bitmaps != null && !bitmaps.isEmpty();
}
private boolean isLoading() {
return activeTasks != null && !activeTasks.isEmpty();
}
private void setupAnimationDrawable() {
AnimationDrawable animationDrawable = new AnimationDrawable();
for (int index = 0; index < bitmaps.size(); index++) {
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmaps.get(index));
animationDrawable.addFrame(drawable, 1000 / framesPerSecond);
}
animationDrawable.setOneShot(!this.loop);
this.setImageDrawable(animationDrawable);
animationDrawable.start();
}
}