hello pauline,
Please note that we have loadImage and loadImage2 now.
loadImage is the old one and is so intelligent to load an image that is not in RAM anymore - clever but bad for our goal.
So I invented loadImage2 which is not so clever and just returns null if we don't have the image in our RAM anymore. Good.
That's why we use loadImage first to load all images and then use loadImage2 to show/retrieve them.
Taking it further
He is always killing image #6 at the moment. You have to make it so that
- when the user goes to the next image he loads one image ahead of the images he has (and kill one image before the images he has) and
- when he goes to the previous image he has to load one image before the images he has (and kill one image after the images he has)
This means you always have a range of images in the RAM:
Index rangeFrom to Index rangeTo.
And you have one image to show.
The image to show is in the middle of the range: imageToShow.
So when you show image 425 imageToShow is 425 and rangeFrom is 420 and rangeTo is 430.
Now when the user scrolls we change imageToShow to 426 or to 427.
We also change rangeFrom and rangeTo (+ or -).
Then we kill a image and load another one.
The reason why this is so complicate is that
- A) we can't hold 900 images in the RAM and
- B) we assume we save time for the user when we keep images in RAM (so there is no delay when he goes fast to the next 3 or 4 images) instead of loading the one image he has just scrolled to
Reason B still let's me think of two threads but this seems hard to do on Android.
But thinking about it: You could do it with a trick.
A false approach would be:
- User goes to the next image, we kill one image, we load one image, we show the new image. This is the obvious approach and it would be much slower than just load the new image and show it (and only have one image in RAM all the time....... what could work too....... did you try it??)
Instead my trick works as this:
- You have a extra variable determing what the program has to do. It's a kind of state of the program. Now when the user scrolls to the next image set state to showNextImage. In draw() now if (or use switch) state == showNextImage show next image (only increase imageToShow and show it). Now the user looks at the the image. Note that in this approach the image already can be seen before we killed or loaded anything. Good. We saved time. We set state to adjustImageRange now. In draw() now if state == adjustImageRange we kill and load images.
Before setup():
- final int doNothing = 0;
- final int showNextImage = 1;
- final int showPrevImage = 2;
- final int adjustImageRangeAfterPrevious = 3;
- final int adjustImageRangeAfterNext = 4;
- //
- int state = doNothing;
In draw():
- switch (state) {
- case doNothing :
- // doing nothing
- break;
- case showNextImage :
- imageToShow ++;
- state = adjustImageRangeAfterNext;
- break;
- case showPrevImage :
- imageToShow --;
- state = adjustImageRangeAfterPrevious;
- break;
- case adjustImageRangeAfterNext:
- // we move our range right (next)
- kill(rangeFrom);
- rangeFrom++;
- rangeTo++;
- load(rangeTo);
- state = doNothing;
- break;
- case adjustImageRangeAfterPrevious:
- // we move our range left (previous)
- kill(rangeTo);
- rangeTo--;
- rangeFrom--;
- load(rangeFrom);
- state = doNothing;
- break;
-
- } // switch
You have to write kill() and load() both based on an index.
You have to check for lower and upper range resp.
The goal of the approach is that when we show the image we don't load or kill anything yet.
Instead, draw() goes on, comes to its end and shows the next image from RAM.
Only in the next round (since we have changed state) it kills and loads (while the user already views the image).
Use
- state = showNextImage;
when the user goes to next image.
Greetings, Chrisir