1
2
3
4
5
6
7
8
9
10
11
12
13
|
ImageView mImageView = (ImageView) findViewById(R.id.image);
String imageUrl =
"https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg"
;
//显示图片的配置
DisplayImageOptions options =
new
DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(
true
)
.cacheOnDisk(
true
)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(imageUrl, mImageView, options);
|
1
2
3
|
public
void
displayImage(String uri, ImageView imageView, DisplayImageOptions options) {
displayImage(uri,
new
ImageViewAware(imageView), options,
null
,
null
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
checkConfiguration();
if
(imageAware ==
null
) {
throw
new
IllegalArgumentException(ERROR_WRONG_ARGUMENTS);
}
if
(listener ==
null
) {
listener = emptyListener;
}
if
(options ==
null
) {
options = configuration.defaultDisplayImageOptions;
}
if
(TextUtils.isEmpty(uri)) {
engine.cancelDisplayTaskFor(imageAware);
listener.onLoadingStarted(uri, imageAware.getWrappedView());
if
(options.shouldShowImageForEmptyUri()) {
imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));
}
else
{
imageAware.setImageDrawable(
null
);
}
listener.onLoadingComplete(uri, imageAware.getWrappedView(),
null
);
return
;
}
|
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
|
ImageSize targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());
String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);
engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);
listener.onLoadingStarted(uri, imageAware.getWrappedView());
Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
if
(bmp !=
null
&& !bmp.isRecycled()) {
L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);
if
(options.shouldPostProcess()) {
ImageLoadingInfo imageLoadingInfo =
new
ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,
options, listener, progressListener, engine.getLockForUri(uri));
ProcessAndDisplayImageTask displayTask =
new
ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo,
defineHandler(options));
if
(options.isSyncLoading()) {
displayTask.run();
}
else
{
engine.submit(displayTask);
}
}
else
{
options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);
listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
if
(options.shouldShowImageOnLoading()) {
imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources));
}
else
if
(options.isResetViewBeforeLoading()) {
imageAware.setImageDrawable(
null
);
}
ImageLoadingInfo imageLoadingInfo =
new
ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,
options, listener, progressListener, engine.getLockForUri(uri));
LoadAndDisplayImageTask displayTask =
new
LoadAndDisplayImageTask(engine, imageLoadingInfo,
defineHandler(options));
if
(options.isSyncLoading()) {
displayTask.run();
}
else
{
engine.submit(displayTask);
}
|
1
2
|
if
(waitIfPaused())
return
;
if
(delayIfNeed())
return
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private
boolean
waitIfPaused() {
AtomicBoolean pause = engine.getPause();
if
(pause.get()) {
synchronized
(engine.getPauseLock()) {
if
(pause.get()) {
L.d(LOG_WAITING_FOR_RESUME, memoryCacheKey);
try
{
engine.getPauseLock().wait();
}
catch
(InterruptedException e) {
L.e(LOG_TASK_INTERRUPTED, memoryCacheKey);
return
true
;
}
L.d(LOG_RESUME_AFTER_PAUSE, memoryCacheKey);
}
}
}
return
isTaskNotActual();
|
1
2
3
|
private
boolean
isTaskNotActual() {
return
isViewCollected() || isViewReused();
}
|
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
|
ReentrantLock loadFromUriLock = imageLoadingInfo.loadFromUriLock;
L.d(LOG_START_DISPLAY_IMAGE_TASK, memoryCacheKey);
if
(loadFromUriLock.isLocked()) {
L.d(LOG_WAITING_FOR_IMAGE_LOADED, memoryCacheKey);
}
loadFromUriLock.lock();
Bitmap bmp;
try
{
checkTaskNotActual();
bmp = configuration.memoryCache.get(memoryCacheKey);
if
(bmp ==
null
|| bmp.isRecycled()) {
bmp = tryLoadBitmap();
if
(bmp ==
null
)
return
;
// listener callback already was fired
checkTaskNotActual();
checkTaskInterrupted();
if
(options.shouldPreProcess()) {
L.d(LOG_PREPROCESS_IMAGE, memoryCacheKey);
bmp = options.getPreProcessor().process(bmp);
if
(bmp ==
null
) {
L.e(ERROR_PRE_PROCESSOR_NULL, memoryCacheKey);
}
}
if
(bmp !=
null
&& options.isCacheInMemory()) {
L.d(LOG_CACHE_IMAGE_IN_MEMORY, memoryCacheKey);
configuration.memoryCache.put(memoryCacheKey, bmp);
}
}
else
{
loadedFrom = LoadedFrom.MEMORY_CACHE;
L.d(LOG_GET_IMAGE_FROM_MEMORY_CACHE_AFTER_WAITING, memoryCacheKey);
}
if
(bmp !=
null
&& options.shouldPostProcess()) {
L.d(LOG_POSTPROCESS_IMAGE, memoryCacheKey);
bmp = options.getPostProcessor().process(bmp);
if
(bmp ==
null
) {
L.e(ERROR_POST_PROCESSOR_NULL, memoryCacheKey);
}
}
checkTaskNotActual();
checkTaskInterrupted();
}
catch
(TaskCancelledException e) {
fireCancelEvent();
return
;
}
finally
{
loadFromUriLock.unlock();
}
|
1
2
3
4
5
6
7
8
|
ReentrantLock getLockForUri(String uri) {
ReentrantLock lock = uriLocks.get(uri);
if
(lock ==
null
) {
lock =
new
ReentrantLock();
uriLocks.put(uri, lock);
}
return
lock;
}
|
1
2
3
4
5
6
7
8
|
File imageFile = configuration.diskCache.get(uri);
if
(imageFile !=
null
&& imageFile.exists()) {
L.d(LOG_LOAD_IMAGE_FROM_DISK_CACHE, memoryCacheKey);
loadedFrom = LoadedFrom.DISC_CACHE;
checkTaskNotActual();
bitmap = decodeImage(Scheme.FILE.wrap(imageFile.getAbsolutePath()));
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
if
(bitmap ==
null
|| bitmap.getWidth() <=
0
|| bitmap.getHeight() <=
0
) {
L.d(LOG_LOAD_IMAGE_FROM_NETWORK, memoryCacheKey);
loadedFrom = LoadedFrom.NETWORK;
String imageUriForDecoding = uri;
if
(options.isCacheOnDisk() && tryCacheImageOnDisk()) {
imageFile = configuration.diskCache.get(uri);
if
(imageFile !=
null
) {
imageUriForDecoding = Scheme.FILE.wrap(imageFile.getAbsolutePath());
}
}
checkTaskNotActual();
bitmap = decodeImage(imageUriForDecoding);
if
(bitmap ==
null
|| bitmap.getWidth() <=
0
|| bitmap.getHeight() <=
0
) {
fireFailEvent(FailType.DECODING_ERROR,
null
);
}
}
|
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
|
private
Bitmap decodeImage(String imageUri)
throws
IOException {
ViewScaleType viewScaleType = imageAware.getScaleType();
ImageDecodingInfo decodingInfo =
new
ImageDecodingInfo(memoryCacheKey, imageUri, uri, targetSize, viewScaleType,
getDownloader(), options);
return
decoder.decode(decodingInfo);
}
/** @return <b>true</b> - if image was downloaded successfully; <b>false</b> - otherwise */
private
boolean
tryCacheImageOnDisk()
throws
TaskCancelledException {
L.d(LOG_CACHE_IMAGE_ON_DISK, memoryCacheKey);
boolean
loaded;
try
{
loaded = downloadImage();
if
(loaded) {
int
width = configuration.maxImageWidthForDiskCache;
int
height = configuration.maxImageHeightForDiskCache;
if
(width >
0
|| height >
0
) {
L.d(LOG_RESIZE_CACHED_IMAGE_FILE, memoryCacheKey);
resizeAndSaveImage(width, height);
// TODO : process boolean result
}
}
}
catch
(IOException e) {
L.e(e);
loaded =
false
;
}
return
loaded;
}
private
boolean
downloadImage()
throws
IOException {
InputStream is = getDownloader().getStream(uri, options.getExtraForDownloader());
return
configuration.diskCache.save(uri, is,
this
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
if
(bmp ==
null
)
return
;
// listener callback already was fired
checkTaskNotActual();
checkTaskInterrupted();
if
(options.shouldPreProcess()) {
L.d(LOG_PREPROCESS_IMAGE, memoryCacheKey);
bmp = options.getPreProcessor().process(bmp);
if
(bmp ==
null
) {
L.e(ERROR_PRE_PROCESSOR_NULL, memoryCacheKey);
}
}
if
(bmp !=
null
&& options.isCacheInMemory()) {
L.d(LOG_CACHE_IMAGE_IN_MEMORY, memoryCacheKey);
configuration.memoryCache.put(memoryCacheKey, bmp);
}
|
1
2
|
DisplayBitmapTask displayBitmapTask =
new
DisplayBitmapTask(bmp, imageLoadingInfo, engine, loadedFrom);
runTask(displayBitmapTask, syncLoading, handler, engine);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Override
public
void
run() {
if
(imageAware.isCollected()) {
L.d(LOG_TASK_CANCELLED_IMAGEAWARE_COLLECTED, memoryCacheKey);
listener.onLoadingCancelled(imageUri, imageAware.getWrappedView());
}
else
if
(isViewWasReused()) {
L.d(LOG_TASK_CANCELLED_IMAGEAWARE_REUSED, memoryCacheKey);
listener.onLoadingCancelled(imageUri, imageAware.getWrappedView());
}
else
{
L.d(LOG_DISPLAY_IMAGE_IN_IMAGEAWARE, loadedFrom, memoryCacheKey);
displayer.display(bitmap, imageAware, loadedFrom);
engine.cancelDisplayTaskFor(imageAware);
listener.onLoadingComplete(imageUri, imageAware.getWrappedView(), bitmap);
}
}
|