public class ImageCache extends ImageMemoryCache
ImageMemoryCache
and
ImageSDCardCache
. It applies to apps those used much images, like sina weibo, twitter, taobao, huaban, weixin
and so on.ImageMemoryCache.setOnImageCallbackListener(OnImageCallbackListener)
set callback interface after image
get successImageMemoryCache.get(String, List, View)
get image asynchronous and preload other images asynchronous
according to urlListImageMemoryCache.get(String, View)
get image asynchronousinitData(Context, String)
or loadDataFromDb(Context, String)
to init data when app start,
saveDataToDb(Context, String)
to save data when app exitsetHttpReadTimeOut(int)
set http read image time out, if less than 0, not set. default is not setPreloadDataCache.setContext(Context)
and setAllowedNetworkTypes(int)
restrict the types of
networks over which this data can get.ImageMemoryCache.setOpenWaitingQueue(boolean)
set whether open waiting queue, default is true. If true,
save all view waiting for image loaded, else only save the newest onePreloadDataCache.setOnGetDataListener(OnGetDataListener)
set how to get image, this cache will get image
and preload images by itSimpleCache.setCacheFullRemoveType(CacheFullRemoveType)
set remove type when primary cache is fullsetCacheFullRemoveTypeOfSecondaryCache(CacheFullRemoveType)
set remove type when secondary cache is full
Modifier and Type | Class and Description |
---|---|
static interface |
ImageCache.CompressListener
set image compress scale
|
ImageMemoryCache.OnImageCallbackListener
PreloadDataCache.OnGetDataListener<K,V>
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_CACHE_FOLDER
cache folder path which be used when saving images
|
DEFAULT_MAX_SIZE
DEFAULT_BACKWARD_CACHE_NUMBER, DEFAULT_FORWARD_CACHE_NUMBER, DEFAULT_THREAD_POOL_SIZE, NETWORK_MOBILE, NETWORK_WIFI
Constructor and Description |
---|
ImageCache()
max size of primary cache is
ImageMemoryCache.DEFAULT_MAX_SIZE , max size of secondary cache is
ImageSDCardCache.DEFAULT_MAX_SIZE
thread pool size of primary cache and secondary cache both are
PreloadDataCache.DEFAULT_THREAD_POOL_SIZE
|
ImageCache(int primaryCacheMaxSize)
max size of secondary cache is
ImageSDCardCache.DEFAULT_MAX_SIZE
thread pool size of primary cache and secondary cache both are
PreloadDataCache.DEFAULT_THREAD_POOL_SIZE
|
ImageCache(int primaryCacheMaxSize,
int secondaryCacheMaxSize)
thread pool size of primary cache and secondary cache both are
PreloadDataCache.DEFAULT_THREAD_POOL_SIZE |
ImageCache(int primaryCacheMaxSize,
int primaryCacheThreadPoolSize,
int secondaryCacheMaxSize,
int secondaryCacheThreadPoolSize)
Callback interface after image get success is null, can set by
PreloadDataCache#setOnImageCallbackListener(OnImageCallbackListener)
Get data listener of primary cache is getOnGetImageListenerOfPrimaryCache() , you can set by
#setOnGetImageListenerOfPrimaryCache(OnGetDataListener) , but not recommended, you may destory secondary
cache. |
Modifier and Type | Method and Description |
---|---|
boolean |
checkIsNetworkTypeAllowed()
Check if get data can proceed over the given network type.
|
void |
clear()
clear both primary cache and secondary cache
|
void |
deleteUnusedFiles()
delete unused file in
getCacheFolder() , you can use it after loadDataFromDb(Context, String) at
first time |
int |
getAllowedNetworkTypes()
get the types of networks over which this data can get
|
java.lang.String |
getCacheFolder()
get cache folder path which be used when saving images, default is
DEFAULT_CACHE_FOLDER |
CacheFullRemoveType<java.lang.String> |
getCacheFullRemoveTypeOfSecondaryCache()
get remove type when secondary cache is full
|
ImageCache.CompressListener |
getCompressListener()
get compressListener
|
int |
getCompressSize()
get compressSize
|
Context |
getContext() |
FileNameRule |
getFileNameRule()
get file name rule which be used when saving images, default is
FileNameRuleImageUrl |
int |
getHttpReadTimeOut()
get http read image time out of secondary cache, if less than 0, not set. default is not set
|
java.lang.String |
getImagePath(java.lang.String imageUrl)
get image file path
|
PreloadDataCache.OnGetDataListener<java.lang.String,Bitmap> |
getOnGetImageListenerOfPrimaryCache()
get get image listener of primary cache
|
PreloadDataCache.OnGetDataListener<java.lang.String,java.lang.String> |
getOnGetImageListenerOfSecondaryCache()
get get image listener of secondary cache
|
java.util.Map<java.lang.String,java.lang.String> |
getRequestProperties()
get http request properties
|
void |
initData(Context context,
java.lang.String tag)
load all data from db and delete unused file in
getCacheFolder()
It's a combination of loadDataFromDb(Context, String) and deleteUnusedFiles()
You should use saveDataToDb(Context, String) to save data when app exit
|
boolean |
isCheckNetwork()
get whether to check the network at first when get data, used when
PreloadDataCache.checkIsNetworkTypeAllowed() |
boolean |
loadDataFromDb(Context context,
java.lang.String tag)
load all data in db whose tag is same to tag to this cache. just put, do not affect the original data
Attentions:
If tag is null or empty, throws exception
You should use
saveDataToDb(Context, String) to save data when app exit
|
boolean |
saveDataToDb(Context context,
java.lang.String tag)
delete all rows in db whose tag is same to tag at first, and insert all data in this cache to db
Attentions:
If tag is null or empty, throws exception
Will delete all rows in db whose tag is same to tag at first
You can use
initData(Context, String) or loadDataFromDb(Context, String) to init data when
app start
|
void |
setAllowedNetworkTypes(int allowedNetworkTypes)
Restrict the types of networks over which this data can get.
|
void |
setBackwardCacheNumber(int backwardCacheNumber)
set count for preload backward, default is
PreloadDataCache.DEFAULT_BACKWARD_CACHE_NUMBER |
void |
setCacheFolder(java.lang.String cacheFolder)
set cache folder path which be used when saving images, default is
DEFAULT_CACHE_FOLDER |
void |
setCacheFullRemoveTypeOfSecondaryCache(CacheFullRemoveType<java.lang.String> cacheFullRemoveType)
set remove type when secondary cache is full
|
void |
setCheckNetwork(boolean isCheckNetwork)
set whether to check the network at first when get data, used when
PreloadDataCache.checkIsNetworkTypeAllowed() |
void |
setCompressListener(ImageCache.CompressListener compressListener)
set compressListener
Attentions:
if this function is set, the function
#setCompressSize(String) is not work
|
void |
setCompressSize(int compressSize)
set image compress scale
Attentions:
if
setCompressListener(CompressListener) is set, this function is not work
|
void |
setContext(Context context)
|
void |
setFileNameRule(FileNameRule fileNameRule)
set file name rule which be used when saving images, default is
FileNameRuleImageUrl |
void |
setForwardCacheNumber(int forwardCacheNumber)
set count for preload forward, default is
PreloadDataCache.DEFAULT_FORWARD_CACHE_NUMBER |
void |
setHttpReadTimeOut(int readTimeOutMillis)
set http read image time out of secondary cache, if less than 0, not set. default is not set, in mills
|
void |
setOnGetImageListenerOfPrimaryCache(PreloadDataCache.OnGetDataListener<java.lang.String,Bitmap> onGetImageListener)
set get data listener of primary cache, primary cache will get data and preload data by it
|
void |
setOnGetImageListenerOfSecondaryCache(PreloadDataCache.OnGetDataListener<java.lang.String,java.lang.String> onGetImageListener)
set get data listener of secondary cache, secondary cache will get data and preload data by it
|
void |
setRequestProperties(java.util.Map<java.lang.String,java.lang.String> requestProperties)
set http request properties
If image is from the different server, setRequestProperty("Connection", "false") is recommended.
|
void |
setRequestProperty(java.lang.String field,
java.lang.String newValue)
Sets the value of the http request header field
|
java.util.List<java.lang.Runnable> |
shutdownNow() |
get, get, getDefaultOnGetImageListener, getOnImageCallbackListener, isOpenWaitingQueue, setOnImageCallbackListener, setOpenWaitingQueue
get, getBackwardCacheNumber, getForwardCacheNumber, getOnGetDataListener, isExistGettingDataThread, loadCache, setOnGetDataListener
containsKey, entrySet, getCacheFullRemoveType, getHitCount, getHitRate, getMaxSize, getMissCount, getSize, getValidTime, keySet, put, put, putAll, remove, saveCache, setCacheFullRemoveType, setValidTime, values
public static final java.lang.String DEFAULT_CACHE_FOLDER
public ImageCache()
ImageMemoryCache.DEFAULT_MAX_SIZE
, max size of secondary cache is
ImageSDCardCache.DEFAULT_MAX_SIZE
PreloadDataCache.DEFAULT_THREAD_POOL_SIZE
#ImageCache(int, int, int, int)}
public ImageCache(int primaryCacheMaxSize)
ImageSDCardCache.DEFAULT_MAX_SIZE
PreloadDataCache.DEFAULT_THREAD_POOL_SIZE
primaryCacheMaxSize
- secondaryCacheMaxSize
- #ImageCache(int, int, int, int)}
public ImageCache(int primaryCacheMaxSize, int secondaryCacheMaxSize)
PreloadDataCache.DEFAULT_THREAD_POOL_SIZE
primaryCacheMaxSize
- secondaryCacheMaxSize
- #ImageCache(int, int, int, int)}
public ImageCache(int primaryCacheMaxSize, int primaryCacheThreadPoolSize, int secondaryCacheMaxSize, int secondaryCacheThreadPoolSize)
PreloadDataCache#setOnImageCallbackListener(OnImageCallbackListener)
getOnGetImageListenerOfPrimaryCache()
, you can set by
#setOnGetImageListenerOfPrimaryCache(OnGetDataListener)
, but not recommended, you may destory secondary
cache.getOnGetImageListenerOfSecondaryCache()
, you can set by
#setOnGetImageListenerOfSecondaryCache(OnGetDataListener)
.RemoveTypeUsedCountSmall
when cache is fullprimaryCacheMaxSize
- maximum size of the primary cacheprimaryCacheThreadPoolSize
- getting data thread pool size of the primary cachesecondaryCacheMaxSize
- maximum size of the secondary cachesecondaryCacheThreadPoolSize
- getting data thread pool size of the secondary cachepublic int getCompressSize()
public void setCompressSize(int compressSize)
setCompressListener(CompressListener)
is set, this function is not workcompressSize
- the compressSize to set#setCompressSize(String)}
public void setCompressListener(ImageCache.CompressListener compressListener)
#setCompressSize(String)
is not workcompressListener
- public ImageCache.CompressListener getCompressListener()
public int getHttpReadTimeOut()
getHttpReadTimeOut
in class ImageMemoryCache
public void setHttpReadTimeOut(int readTimeOutMillis)
setHttpReadTimeOut
in class ImageMemoryCache
readTimeOutMillis
- public void clear()
clear
in interface Cache<java.lang.String,Bitmap>
clear
in class SimpleCache<java.lang.String,Bitmap>
Map.clear()
public void setForwardCacheNumber(int forwardCacheNumber)
PreloadDataCache
PreloadDataCache.DEFAULT_FORWARD_CACHE_NUMBER
setForwardCacheNumber
in class PreloadDataCache<java.lang.String,Bitmap>
public void setBackwardCacheNumber(int backwardCacheNumber)
PreloadDataCache
PreloadDataCache.DEFAULT_BACKWARD_CACHE_NUMBER
setBackwardCacheNumber
in class PreloadDataCache<java.lang.String,Bitmap>
public int getAllowedNetworkTypes()
PreloadDataCache
getAllowedNetworkTypes
in class PreloadDataCache<java.lang.String,Bitmap>
public void setAllowedNetworkTypes(int allowedNetworkTypes)
PreloadDataCache
PreloadDataCache.getContext()
is not nullsetAllowedNetworkTypes
in class PreloadDataCache<java.lang.String,Bitmap>
allowedNetworkTypes
- any combination of the NETWORK_* bit flags.public boolean isCheckNetwork()
PreloadDataCache
PreloadDataCache.checkIsNetworkTypeAllowed()
isCheckNetwork
in class PreloadDataCache<java.lang.String,Bitmap>
public void setCheckNetwork(boolean isCheckNetwork)
PreloadDataCache
PreloadDataCache.checkIsNetworkTypeAllowed()
setCheckNetwork
in class PreloadDataCache<java.lang.String,Bitmap>
public boolean checkIsNetworkTypeAllowed()
PreloadDataCache
checkIsNetworkTypeAllowed
in class PreloadDataCache<java.lang.String,Bitmap>
PreloadDataCache.getContext()
is null, return truePreloadDataCache.getAllowedNetworkTypes()
is not match network, return falsepublic Context getContext()
getContext
in class PreloadDataCache<java.lang.String,Bitmap>
public void setContext(Context context)
PreloadDataCache
setContext
in class PreloadDataCache<java.lang.String,Bitmap>
public void setRequestProperties(java.util.Map<java.lang.String,java.lang.String> requestProperties)
setRequestProperties
in class ImageMemoryCache
requestProperties
- public java.util.Map<java.lang.String,java.lang.String> getRequestProperties()
getRequestProperties
in class ImageMemoryCache
public void setRequestProperty(java.lang.String field, java.lang.String newValue)
setRequestProperty
in class ImageMemoryCache
field
- the request header field to be setnewValue
- the new value of the specified property#setRequestProperties(Map)}
public java.lang.String getCacheFolder()
DEFAULT_CACHE_FOLDER
ImageSDCardCache.getCacheFolder()
public void setCacheFolder(java.lang.String cacheFolder)
DEFAULT_CACHE_FOLDER
cacheFolder
- ImageSDCardCache.setCacheFolder(String)
public FileNameRule getFileNameRule()
FileNameRuleImageUrl
ImageSDCardCache.getFileNameRule()
public void setFileNameRule(FileNameRule fileNameRule)
FileNameRuleImageUrl
fileNameRule
- ImageSDCardCache.setFileNameRule(FileNameRule)
public void initData(Context context, java.lang.String tag)
getCacheFolder()
loadDataFromDb(Context, String)
and deleteUnusedFiles()
saveDataToDb(Context, String)
to save data when app exitcontext
- tag
- loadDataFromDb(Context, String)
,
deleteUnusedFiles()
public void deleteUnusedFiles()
getCacheFolder()
, you can use it after loadDataFromDb(Context, String)
at
first timeImageSDCardCache#deleteUnusedFiles()}
public boolean loadDataFromDb(Context context, java.lang.String tag)
saveDataToDb(Context, String)
to save data when app exitcontext
- tag
- tag used to mark this cache when save to and load from db, should be unique and cannot be null or
emptyImageSDCardCache.loadDataFromDb(Context, ImageSDCardCache, String)
public boolean saveDataToDb(Context context, java.lang.String tag)
initData(Context, String)
or loadDataFromDb(Context, String)
to init data when
app startcontext
- tag
- tag used to mark this cache when save to and load from db, should be unique and cannot be null or
emptyImageSDCardCache.saveDataToDb(Context, ImageSDCardCache, String)
public java.lang.String getImagePath(java.lang.String imageUrl)
imageUrl
- public java.util.List<java.lang.Runnable> shutdownNow()
shutdownNow
in class ImageMemoryCache
ExecutorService.shutdownNow()
public PreloadDataCache.OnGetDataListener<java.lang.String,Bitmap> getOnGetImageListenerOfPrimaryCache()
PreloadDataCache#getOnGetDataListener()}
public void setOnGetImageListenerOfPrimaryCache(PreloadDataCache.OnGetDataListener<java.lang.String,Bitmap> onGetImageListener)
onGetImageListener
- PreloadDataCache#setOnGetDataListener(OnGetDataListener)}
public PreloadDataCache.OnGetDataListener<java.lang.String,java.lang.String> getOnGetImageListenerOfSecondaryCache()
public void setOnGetImageListenerOfSecondaryCache(PreloadDataCache.OnGetDataListener<java.lang.String,java.lang.String> onGetImageListener)
onGetImageListener
- public CacheFullRemoveType<java.lang.String> getCacheFullRemoveTypeOfSecondaryCache()
public void setCacheFullRemoveTypeOfSecondaryCache(CacheFullRemoveType<java.lang.String> cacheFullRemoveType)
cacheFullRemoveType
- the cacheFullRemoveType to set