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
|
class ImageRef
{
WeakReference<Bitmap> memory;
File cache;
URL url;
ArrayList<INotificatioN> listeners;
public Bitmap getBitmap(INotification notifier)
{
Bitmap ret = (this.memory == null) ? null : this.memory.get();
if (ret == null)
startBitmapLoading(notifier);
return (ret == null) ? Bitmap.LOADING : ret;
}
public void startBitmapLoading(INotification notifier)
{
this.listeners.add(notifier);
if (this.loader != null) return;
if (this.cache != null)
this.loader = new AsyncFileLoader();
else
this.loader = new AsyncURLLoader();
this.loader.execute(this);
}
public void doLoadFromURL()
{
// transfer from URL to File
return doLoadFromFile();
}
public void doLoadFromFile()
{
// transfer from File to Bitmap
this.memory = new WeakReference<Bitmap>(result);
}
public void notifyListeners()
{
for (INotification n : this.listeners)
n.bitmapLoaded();
this.listeners.clear();
}
static abstract class AsyncImageLoader extends AsyncTask<ImageRef,Void,ImageRef[]>
{
public void onPostExecute(ImageRef[] refs)
{
for (ImageRef r : refs)
r.notifyListeners();
}
}
static class AsyncFileLoader extends AsyncImageLoader
{
public ImageRef[] doInBackground(ImageRef ... refs)
{
for (ImageRef r : refs)
r.doLoadFromFile();
return refs;
}
}
static class AsyncURLLoader extends AsyncImageLoader
{
public ImageRef[] doInBackground(ImageRef ... refs)
{
for (ImageRef r : refs)
r.doLoadFromUrl();
return refs;
}
}
} |
Partager