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

[TIMOB-18564]: Caching images animating in a loop #6689

Merged
merged 2 commits into from
Mar 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ArrayBlockingQueue;
Expand Down Expand Up @@ -53,7 +54,6 @@ public class TiUIImageView extends TiUIView implements OnLifecycleEvent, Handler
{
private static final String TAG = "TiUIImageView";
private static final int FRAME_QUEUE_SIZE = 5;
public static final int INFINITE = 0;
public static final int MIN_DURATION = 30;
public static final int DEFAULT_DURATION = 200;

Expand Down Expand Up @@ -278,19 +278,21 @@ public BitmapWithIndex(Bitmap b, int i)
private class Loader implements Runnable
{
private ArrayBlockingQueue<BitmapWithIndex> bitmapQueue;
private LinkedList<Integer> hashTable;
private int waitTime = 0;
private int sleepTime = 50; //ms
private int repeatIndex = 0;

public Loader()
{
bitmapQueue = new ArrayBlockingQueue<BitmapWithIndex>(FRAME_QUEUE_SIZE);
hashTable = new LinkedList<Integer>();
}

private boolean isRepeating()
{
int repeatCount = getRepeatCount();
if (repeatCount <= INFINITE) {
if (repeatCount <= 0) {
return true;
}
return repeatIndex < repeatCount;
Expand Down Expand Up @@ -338,6 +340,7 @@ public void run()
repeatIndex = 0;
isLoading.set(true);
firedLoad = false;
boolean shouldCache = getRepeatCount() >= 5 ? true : false;
topLoop: while (isRepeating()) {

if (imageSources == null) {
Expand Down Expand Up @@ -381,7 +384,20 @@ public void run()
if (imageSources == null || j >= imageSources.size()) {
break topLoop;
}
Bitmap b = imageSources.get(j).getBitmap(true);
TiDrawableReference imageRef = imageSources.get(j);
Bitmap b = null;
if (shouldCache) {
int hash = imageRef.hashCode();
b = mMemoryCache.get(hash);
if (b == null) {
Log.i(TAG, "Image isn't cached");
b = imageRef.getBitmap(true);
mMemoryCache.put(hash, b);
hashTable.add(hash);
}
} else {
b = imageRef.getBitmap(true);
}
BitmapWithIndex bIndex = new BitmapWithIndex(b,j);
while (waitTime < getDuration() * imageSources.size()) {
try {
Expand Down Expand Up @@ -409,6 +425,10 @@ public void run()

}
isLoading.set(false);
//clean out the cache after animation
while (!hashTable.isEmpty()) {
mMemoryCache.remove(hashTable.pop());
}
}

public ArrayBlockingQueue<BitmapWithIndex> getBitmapQueue()
Expand Down Expand Up @@ -456,7 +476,7 @@ public int getRepeatCount()
if (proxy.hasProperty(TiC.PROPERTY_REPEAT_COUNT)) {
return TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_REPEAT_COUNT));
}
return INFINITE;
return 0;
}

private void fireLoad(String state)
Expand Down Expand Up @@ -517,6 +537,7 @@ public void run()
wait();
}
}

ArrayBlockingQueue<BitmapWithIndex> bitmapQueue = loader.getBitmapQueue();
//Fire stop event when animation finishes
if (!isLoading.get() && bitmapQueue.isEmpty()) {
Expand Down