本文整理匯總了Java中android.util.LruCache類的典型用法代碼示例。如果您正苦於以下問題:Java LruCache類的具體用法?Java LruCache怎麽用?Java LruCache使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
LruCache類屬於android.util包,在下文中一共展示了LruCache類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createLoadingHeader
import android.util.LruCache; //導入依賴的package包/類
/**
* @param type 數據類型
* @return 根據type產生loading的頭
*/
private T createLoadingHeader(int type) {
checkLoadingAdapterBind();
if (mSingleCache == null) {
mSingleCache = new LruCache<String, T>(mMaxSingleCacheCount);
}
String headerKey = getHeaderKey(type);
T header = mSingleCache.get(headerKey);
if (header == null) {
header = mLoadingEntityAdapter.createLoadingHeaderEntity(type);
mSingleCache.put(headerKey, header);
}
mLoadingEntityAdapter.bindLoadingEntity(header, -1);
return header;
}
示例2: MemoryLruCache
import android.util.LruCache; //導入依賴的package包/類
private MemoryLruCache(){
int maxMemorySize= (int) (Runtime.getRuntime().maxMemory()/16);
Log.i(GlobalConfig.TAG,"內存可用的大小:"+maxMemorySize/1024 + "K");
if (maxMemorySize <= 0){
maxMemorySize = 10*1024*1024;
}
lruCache = new LruCache<String, Bitmap>(maxMemorySize){
@Override
protected int sizeOf(String key, Bitmap value) {
//一張圖片的大小
return value.getRowBytes()*value.getHeight();
}
};
}
示例3: getSectionData
import android.util.LruCache; //導入依賴的package包/類
private SparseArray<ArrayList<String>> getSectionData(int section, PageProperty property) {
SparseArray<ArrayList<String>> pages = null;
if (map == null) {
map = new LruCache<>(3);
pages = loadPages(getPageSource(section), property.textPaint, property.visibleHeight, property.visibleWidth, property.intervalSize, property.paragraphSize);
map.put(section, pages);
mPageProperty=property;
} else {
if (mPageProperty != null && mPageProperty.equals(property)) {
pages = map.get(section);
}
if (pages == null) {
pages = loadPages(getPageSource(section), property.textPaint, property.visibleHeight, property.visibleWidth, property.intervalSize, property.paragraphSize);
map.put(section, pages);
mPageProperty=property;
}
}
return pages;
}
示例4: ImageLoader
import android.util.LruCache; //導入依賴的package包/類
private ImageLoader(Context mContext, String dirNameByRoot) {
this.mContext = mContext;
if (dirNameByRoot != null && dirNameByRoot.length() > 0)
fileUtils = new FileUtils(mContext, dirNameByRoot);
else
fileUtils = new FileUtils(mContext);
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// 給LruCache分配1/8 4M
int mCacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(mCacheSize) {
// 必須重寫此方法,來測量Bitmap的大小
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
}
示例5: ClusterOverlay
import android.util.LruCache; //導入依賴的package包/類
/**
* 構造函數,批量添加聚合元素時,調用此構造函數
*
* @param amap
* @param clusterItems 聚合元素
* @param clusterSize
* @param context
*/
public ClusterOverlay(AMap amap, List<ClusterItem> clusterItems,
int clusterSize, Context context) {
//默認最多會緩存80張圖片作為聚合顯示元素圖片,根據自己顯示需求和app使用內存情況,可以修改數量
mLruCache = new LruCache<Integer, Drawable>(80) {
};
if (clusterItems != null) {
mClusterItems = clusterItems;
} else {
mClusterItems = new ArrayList<ClusterItem>();
}
mContext = context;
mClusters = new ArrayList<Cluster>();
this.mAMap = amap;
mClusterSize = clusterSize;
mPXInMeters = mAMap.getScalePerPixel();
mClusterDistance = mPXInMeters * mClusterSize;
amap.setOnCameraChangeListener(this);
amap.setOnMarkerClickListener(this);
setClusterRenderer(this);
setOnClusterClickListener(this);
initThreadHandler();
assignClusters();
}
示例6: AppMuteManager
import android.util.LruCache; //導入依賴的package包/類
public AppMuteManager(NotificationService service) {
this.service = service;
googleApiClient = new GoogleApiClient.Builder(service)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
final int maxMemory = (int) (Runtime.getRuntime().maxMemory());
iconCache = new LruCache<String, Bitmap>(maxMemory / 32) // 1/16th of device's RAM should be far enough for all icons
{
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
示例7: onCreate
import android.util.LruCache; //導入依賴的package包/類
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
systemApps = getArguments().getBoolean("systemApps", false);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
iconCache = new LruCache<String, Bitmap>(maxMemory / 16) // 1/16th of device's RAM should be far enough for all icons
{
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount() / 1024;
}
};
adapter = new AppListAdapter();
}
示例8: BitmapLruCache
import android.util.LruCache; //導入依賴的package包/類
public BitmapLruCache(int cacheSize) {
mCache = new LruCache<T, Bitmap>(cacheSize) {
@Override
protected int sizeOf(T id, Bitmap value) {
return value.getByteCount();
}
@Override
protected void entryRemoved(boolean evicted, T key, Bitmap oldValue,
Bitmap newValue) {
if (oldValue.isMutable()) {
oldValue.recycle();
}
}
};
}
示例9: init
import android.util.LruCache; //導入依賴的package包/類
public static void init(Context context) {
if (sFonts != null) {
return;
}
sCache = new LruCache<String, MemoryFile>(FontProviderSettings.getMaxCache()) {
@Override
protected void entryRemoved(boolean evicted, String key, MemoryFile oldValue, MemoryFile newValue) {
if (evicted) {
oldValue.close();
}
}
@Override
protected int sizeOf(String key, MemoryFile value) {
return value.length();
}
};
sFonts = new ArrayList<>();
for (int res : FONTS_RES) {
FontInfo font = new Gson().fromJson(new InputStreamReader(context.getResources().openRawResource(res)), FontInfo.class);
sFonts.add(font);
}
}
示例10: ImageCache
import android.util.LruCache; //導入依賴的package包/類
public ImageCache() {
mReusableBitmaps =
Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>());
long memCacheSize = Runtime.getRuntime().freeMemory() / 8;
if (memCacheSize <= 0) {
memCacheSize = 1;
}
// If you're running on Honeycomb or newer, create a
// synchronized HashSet of references to reusable bitmaps.
mMemoryCache = new LruCache<String, BitmapDrawable>((int) memCacheSize) {
// // Notify the removed entry that is no longer being cached.
// @Override
// protected void entryRemoved(boolean evicted, String key,
// BitmapDrawable oldValue, BitmapDrawable newValue) {
// //Log.i("TAG","mReusableBitmaps add2");
// //mReusableBitmaps.add(new SoftReference<>(oldValue.getBitmap()));
// }
@Override
protected int sizeOf(String key, BitmapDrawable value) {
return value.getBitmap().getByteCount();
}
};
}
示例11: MemCache
import android.util.LruCache; //導入依賴的package包/類
private MemCache() {
// Find out maximum memory available to application
// 1024 is used because LruCache constructor takes int in kilobytes
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/16th of the available memory for this memory cache.
final int cacheSize = maxMemory / 16;
Log.d(getClass().getName(), "max memory " + maxMemory + " cache size " + cacheSize);
this.memCache = new LruCache<String, Entry>(cacheSize) {
@Override
protected int sizeOf(String key, Entry value) {
return super.sizeOf(key, value);
}
};
}
示例12: ImageLoader
import android.util.LruCache; //導入依賴的package包/類
protected ImageLoader() {
// 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 / LRU_CACHE_SIZE_FACTOR;
memCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap image) {
return image.getByteCount()/1024;
}
};
}
示例13: ImageLoader
import android.util.LruCache; //導入依賴的package包/類
private ImageLoader(Context context) {
mContext = context.getApplicationContext();
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}
};
File diskCacheDir = getDiskCacheDir(mContext, "bitmap");
if (!diskCacheDir.exists()) {
diskCacheDir.mkdirs();
}
if (getUsableSpace(diskCacheDir) > DISK_CACHE_SIZE) {
try {
mDiskLruCache = DiskLruCache.open(diskCacheDir, 1, 1, DISK_CACHE_SIZE);
mIsDiskLruCacheCreated = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例14: onCreate
import android.util.LruCache; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
if (getIntent() != null) {
handleIntent(getIntent());
}
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
VerticalDividerItemDecoration verticalDividerItemDecoration = new VerticalDividerItemDecoration(10);
recyclerView.addItemDecoration(verticalDividerItemDecoration);
this.bitmapLruCache = new LruCache<String, Bitmap>(cacheSize) {
};
adapter = new BookAdapter(bookList, bitmapLruCache);
recyclerView.setAdapter(adapter);
}
示例15: init
import android.util.LruCache; //導入依賴的package包/類
/**
* 初始化
*
* @param threadCount
* @param type
*/
private void init(int threadCount, Type type) {
initBackThread();
// 獲取我們應用的最大可用內存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheMemory = maxMemory / 8;
mLruCache = new LruCache<String, Bitmap>(cacheMemory) {
@Override
protected int sizeOf(String key, Bitmap value) {
// return value.getRowBytes() * value.getHeight();
return value.getByteCount();
}
};
// 創建線程池
mThreadPool = Executors.newFixedThreadPool(threadCount);
mTaskQueue = new LinkedList<Runnable>();
mType = type;
mSemaphoreThreadPool = new Semaphore(threadCount);
}