-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathRCTImageSequenceView.java
More file actions
223 lines (178 loc) · 7.5 KB
/
RCTImageSequenceView.java
File metadata and controls
223 lines (178 loc) · 7.5 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package dk.madslee.imageSequence;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
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;
public class RCTImageSequenceView extends ImageView {
private final Handler handler = new Handler();
private Integer framesPerSecond = 24;
private Boolean loop = true;
private Integer downsampleWidth = -1;
private Integer downsampleHeight = -1;
private ArrayList<AsyncTask> activeTasks = null;
private HashMap<Integer, Bitmap> bitmaps = null;
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) {
Resources res = this.context.getResources();
int resId = resourceDrawableIdHelper.getResourceDrawableId(this.context, uri);
if (downsampleWidth <= 0 || downsampleHeight <= 0) {
// Downsampling is not set so just decode normally
return BitmapFactory.decodeResource(res, resId);
}
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = RCTImageSequenceView.calculateInSampleSize(options, downsampleWidth, downsampleHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
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 static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public void setImages(final ArrayList<String> uris) {
// Cancel any previously queued Runnables
handler.removeCallbacksAndMessages(null);
if (isLoading()) {
// Cancel ongoing tasks (if still loading previous images)
for (int index = 0; index < activeTasks.size(); index++) {
activeTasks.get(index).cancel(true);
}
}
activeTasks = null;
bitmaps = null;
final Runnable r = new Runnable() {
public void run() {
activeTasks = new ArrayList<>(uris.size());
bitmaps = new HashMap<>(uris.size());
for (int index = 0; index < uris.size(); index++) {
DownloadImageTask task = new DownloadImageTask(index, uris.get(index), getContext());
activeTasks.add(task);
try {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (RejectedExecutionException e){
Log.e("react-native-image-sequence", "DownloadImageTask failed" + e.getMessage());
break;
}
}
}
};
// Delay for 1ms to make sure that all the props have been set properly before starting processing
final boolean added = handler.postDelayed(r, 1);
if (!added) {
Log.e("react-native-image-sequence", "Failed to place Runnable in to the message queue");
}
}
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();
}
}
public void setDownsampleWidth(Integer downsampleWidth) {
this.downsampleWidth = downsampleWidth;
}
public void setDownsampleHeight(Integer downsampleHeight) {
this.downsampleHeight = downsampleHeight;
}
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();
}
}