本文整理汇总了Java中android.support.v4.util.LruCache类的典型用法代码示例。如果您正苦于以下问题:Java LruCache类的具体用法?Java LruCache怎么用?Java LruCache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LruCache类属于android.support.v4.util包,在下文中一共展示了LruCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initialize
import android.support.v4.util.LruCache; //导入依赖的package包/类
public static synchronized void initialize(Configuration configuration) {
if (sIsInitialized) {
Log.v("ActiveAndroid already initialized.");
return;
}
sContext = configuration.getContext();
sModelInfo = new ModelInfo(configuration);
sDatabaseHelper = new DatabaseHelper(configuration);
// TODO: It would be nice to override sizeOf here and calculate the memory
// actually used, however at this point it seems like the reflection
// required would be too costly to be of any benefit. We'll just set a max
// object size instead.
sEntities = new LruCache<String, Model>(configuration.getCacheSize());
openDatabase();
sIsInitialized = true;
Log.v("ActiveAndroid initialized successfully.");
}
示例2: initMemCache
import android.support.v4.util.LruCache; //导入依赖的package包/类
private void initMemCache() {
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
}
示例3: EmojiCache
import android.support.v4.util.LruCache; //导入依赖的package包/类
public EmojiCache(int cacheSize) {
mCache = new LruCache<Integer, Drawable>(cacheSize) {
@Override
protected int sizeOf(Integer key, Drawable value) {
return 1;
};
@Override
protected void entryRemoved(boolean evicted, Integer key, Drawable oldValue, Drawable newValue) {
//这种情况,可能该drawable还在页面使用中,不能随便recycle。这里解除引用即可,gc会自动清除
// if (oldValue instanceof BitmapDrawable) {
// ((BitmapDrawable)oldValue).getBitmap().recycle();
// }
}
};
}
示例4: getBitmapCache
import android.support.v4.util.LruCache; //导入依赖的package包/类
private static LruCache<String, Bitmap> getBitmapCache() {
ThreadUtils.assertOnUiThread();
LruCache<String, Bitmap> cache = sBitmapCache == null ? null : sBitmapCache.get();
if (cache != null) return cache;
// Create a new weakly-referenced cache.
cache = new LruCache<String, Bitmap>(MAX_CACHE_BYTES) {
@Override
protected int sizeOf(String key, Bitmap thumbnail) {
return thumbnail == null ? 0 : thumbnail.getByteCount();
}
};
sBitmapCache = new WeakReference<>(cache);
return cache;
}
示例5: setupCaches
import android.support.v4.util.LruCache; //导入依赖的package包/类
private void setupCaches() {
// Titles and subtitles are small enough to stay cached, so use a very high max size
bodyTitleCache = new LruCache<>(10000);
bodySubtitleCache = new LruCache<>(10000);
// Every artwork item will be a BitmapDrawable, so use the bitmap byte count for sizing
bodyArtworkCache = new LruCache<LibraryItem, Drawable>(1000000) {
@Override
protected int sizeOf(final LibraryItem key, final Drawable value) {
return ((BitmapDrawable) value).getBitmap().getByteCount();
}
};
// Header cache will only contain one item
headerTitleCache = new LruCache<>(2);
headerSubtitleCache = new LruCache<>(2);
headerArtworkCache = new LruCache<>(2);
}
示例6: setup
import android.support.v4.util.LruCache; //导入依赖的package包/类
/**
* Initialises the testing objects and assigns them to member variables.
*/
@Before
public void setup() throws LibraryReadException {
artwork = mock(Drawable.class);
when(artwork.getIntrinsicWidth()).thenReturn(100);
when(artwork.getIntrinsicHeight()).thenReturn(100);
defaultArtwork = mock(Drawable.class);
when(defaultArtwork.getIntrinsicWidth()).thenReturn(100);
when(defaultArtwork.getIntrinsicHeight()).thenReturn(100);
cachedArtwork = mock(Drawable.class);
when(cachedArtwork.getIntrinsicWidth()).thenReturn(100);
when(cachedArtwork.getIntrinsicHeight()).thenReturn(100);
libraryItem = mock(LibraryItem.class);
when(libraryItem.getArtwork(anyInt(), anyInt())).thenReturn(artwork);
cache = new LruCache<>(10);
displayableDefaults = mock(DisplayableDefaults.class);
when(displayableDefaults.getArtwork()).thenReturn(defaultArtwork);
imageView = mock(ImageView.class);
}
示例7: setup
import android.support.v4.util.LruCache; //导入依赖的package包/类
/**
* Initialises the testing objects and assigns them to member variables.
*/
@Before
public void setup() throws LibraryReadException {
subtitle = mock(CharSequence.class);
defaultSubtitle = mock(CharSequence.class);
cachedSubtitle = mock(CharSequence.class);
libraryItem = mock(LibraryItem.class);
when(libraryItem.getSubtitle()).thenReturn(subtitle);
cache = new LruCache<>(10);
displayableDefaults = mock(DisplayableDefaults.class);
when(displayableDefaults.getSubtitle()).thenReturn(defaultSubtitle);
textView = mock(TextView.class);
}
示例8: setup
import android.support.v4.util.LruCache; //导入依赖的package包/类
/**
* Initialises the testing objects and assigns them to member variables.
*/
@Before
public void setup() throws LibraryReadException {
title = mock(CharSequence.class);
defaultTitle = mock(CharSequence.class);
cachedTitle = mock(CharSequence.class);
libraryItem = mock(LibraryItem.class);
when(libraryItem.getTitle()).thenReturn(title);
cache = new LruCache<>(10);
displayableDefaults = mock(DisplayableDefaults.class);
when(displayableDefaults.getTitle()).thenReturn(defaultTitle);
textView = mock(TextView.class);
}
示例9: init
import android.support.v4.util.LruCache; //导入依赖的package包/类
/**
* 初始化<硬盘缓存>和<内存缓存>
* @param context context
* @Description:
*/
private void init(Context context)
{
//设置硬盘缓存
mImageDiskCache =
ImageDiskLruCache.openImageCache(context, CacheConfig.Image.DISK_CACHE_NAME, CacheConfig.Image.DISK_CACHE_MAX_SIZE);
// 设置内存缓存.
final int imageMemorySize = CacheUtils.getMemorySize(context, CacheConfig.Image.MEMORY_SHRINK_FACTOR);
LogUtil.d(TAG, "memory size : " + imageMemorySize);
mMemoryCache = new LruCache<String, Bitmap>(imageMemorySize)
{
@Override
protected int sizeOf(String key, Bitmap bitmap)
{
return CacheUtils.getBitmapSize(bitmap);
}
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue)
{
}
};
}
示例10: EmojiCache
import android.support.v4.util.LruCache; //导入依赖的package包/类
public EmojiCache(int cacheSize) {
mCache = new LruCache<Integer, Drawable>(cacheSize) {
@Override
protected int sizeOf(Integer key, Drawable value) {
return 1;
}
@Override
protected void entryRemoved(boolean evicted, Integer key, Drawable oldValue, Drawable newValue) {
//这种情况,可能该drawable还在页面使用中,不能随便recycle。这里解除引用即可,gc会自动清除
// if (oldValue instanceof BitmapDrawable) {
// ((BitmapDrawable)oldValue).getBitmap().recycle();
// }
}
};
}
示例11: JkImageLoader
import android.support.v4.util.LruCache; //导入依赖的package包/类
/**
* Constructor
* @author leibing
* @createTime 2017/3/2
* @lastModify 2017/3/2
* @param context ref
* @return
*/
private JkImageLoader(Context context){
// init application conext
mApplicationContext = context.getApplicationContext();
// init memory cache(one 8 of the max memory)
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
// init disk cache
initDiskCache();
}
示例12: init
import android.support.v4.util.LruCache; //导入依赖的package包/类
/**
* Initialize the cache.
*
* @param memCacheSizePercent The cache size as a percent of available app memory.
*/
private void init(float memCacheSizePercent) {
int memCacheSize = calculateMemCacheSize(memCacheSizePercent);
// Set up memory cache
if (BuildConfig.DEBUG) {
Log.d(TAG, "Memory cache created (size = " + memCacheSize + ")");
}
mMemoryCache = new LruCache<String, Bitmap>(memCacheSize) {
/**
* Measure item size in kilobytes rather than units which is more practical
* for a bitmap cache
*/
@Override
protected int sizeOf(String key, Bitmap bitmap) {
final int bitmapSize = getBitmapSize(bitmap) / 1024;
return bitmapSize == 0 ? 1 : bitmapSize;
}
};
}
示例13: createBitmapCache
import android.support.v4.util.LruCache; //导入依赖的package包/类
/**
* create thumbnail cache
*/
@SuppressLint("NewApi")
private final void createBitmapCache(final boolean clear) {
if (clear && (sThumbnailCache != null)) {
clearBitmapCache(hashCode());
}
if (sThumbnailCache == null) {
// use 1/CACHE_RATE of available memory as memory cache
final int cacheSize = 1024 * 1024 * mMemClass / CACHE_RATE; // [MB] => [bytes]
sThumbnailCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// control memory usage instead of bitmap counts
return bitmap.getRowBytes() * bitmap.getHeight(); // [bytes]
}
};
}
ThreadPool.preStartAllCoreThreads();
}
示例14: settingCache
import android.support.v4.util.LruCache; //导入依赖的package包/类
public static void settingCache() {
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
}
示例15: MemoryCacheManager
import android.support.v4.util.LruCache; //导入依赖的package包/类
private MemoryCacheManager() {
// 取本虚拟机可用的最大内存的1/16作为内存缓存区
int maxMemorySize = (int)Runtime.getRuntime().maxMemory();
int maxCacheSize = maxMemorySize >> 4;
mMemoryLruCache = new LruCache<String, T>(maxCacheSize) {
@Override
protected int sizeOf(String key, T value) {
return value.getSize();
}
@Override
protected void entryRemoved(boolean evicted, String key,
T oldValue, T newValue) {
if (oldValue != null)
oldValue.release();
}
};
}