Android Open Source - SimpleReader Memory Cache From Project Back to project page SimpleReader .
License The source code is released under:
Apache License
If you think the Android project SimpleReader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code package com.dreamteam.app.img;
/ * f r o m w w w . j a v a 2 s . c o m * /
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.graphics.Bitmap;
import com.litesuits.android.log.Log;
public class MemoryCache {
private Map<String, Bitmap> cache = Collections.synchronizedMap(
new LinkedHashMap<String, Bitmap>(10, 1.5f, true));
// ?????????????????0?????????????????????????
private long size = 0;
// ?????????????
private long limit = 1000000;
public MemoryCache() {
// ????????????????25%
setLimit(Runtime.getRuntime().maxMemory()/10);
}
public void setLimit(long limit) {
this.limit = limit;
Log.w("MemoryCache" , "memory cache of image will set to : " + limit / 1024 /1024 + "MB" );
}
public Bitmap getCache(String key) {
if (key == null || "" .equals(key)) {
Log.e("MemoryCache" , "parameter key is null" );
return null;
}
if (cache.containsKey(key)) {
return cache.get(key);
}else {
return null;
}
}
public void put(String key, Bitmap bitmap) {
if (key == null || bitmap == null) {
Log.e("MemoryCache" , "parameter cannot be null" );
return ;
}
if (cache.containsKey(key)) {
return ;
}else {
cache.put(key, bitmap);
size += getSizeInBytes(bitmap);
checkCacheSize();
}
}
public void clear() {
cache.clear();
}
public void checkCacheSize() {
Log.w("MemoryCache" , "check cache..." );
Log.w("MemoryCache" , "cache map size : " + size + " ,map length : " + cache.size());
if (size >= limit) {
Iterator<Entry<String, Bitmap>> iter = cache.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Bitmap> entry = iter.next();
size -= getSizeInBytes(entry.getValue());
iter.remove();
if (size < limit)
break ;
}
Log.w("MemoryCache" , "Clean cache. New size " + cache.size());
}
}
public long getSizeInBytes(Bitmap bitmap) {
if (bitmap == null) {
Log.e("MemoryCache" , "parameter bitmap is null" );
return -1;
}
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
Java Source Code List com.dreamteam.app.adapter.CategoryDetailAdapter.java com.dreamteam.app.adapter.FeedCategoryAdapter.java com.dreamteam.app.adapter.GridAdapter.java com.dreamteam.app.adapter.GuideViewPagerAdapter.java com.dreamteam.app.adapter.ItemListAdapter.java com.dreamteam.app.adapter.MPagerAdapter.java com.dreamteam.app.commons.AppConfig.java com.dreamteam.app.commons.AppContext.java com.dreamteam.app.commons.HtmlFilter.java com.dreamteam.app.commons.IFlyHelper.java com.dreamteam.app.commons.ItemListEntityParser.java com.dreamteam.app.commons.SectionHelper.java com.dreamteam.app.commons.SeriaHelper.java com.dreamteam.app.commons.SkinManager.java com.dreamteam.app.commons.UIHelper.java com.dreamteam.app.config.Contants.java com.dreamteam.app.db.DbManager.java com.dreamteam.app.db.FavoItemDbHelper.java com.dreamteam.app.db.FeedDBManager.java com.dreamteam.app.db.provider.RSSFeedCategoryProvider.java com.dreamteam.app.entity.FeedItem.java com.dreamteam.app.entity.Feed.java com.dreamteam.app.entity.ItemListEntity.java com.dreamteam.app.entity.RSSFeedCategroy.java com.dreamteam.app.entity.Section.java com.dreamteam.app.img.FileCacheManager.java com.dreamteam.app.img.FileCache.java com.dreamteam.app.img.ICache.java com.dreamteam.app.img.ImageLoadTask.java com.dreamteam.app.img.ImageLoad.java com.dreamteam.app.img.ImageLoader.java com.dreamteam.app.img.MemoryCache.java com.dreamteam.app.rss.Dates.java com.dreamteam.app.rss.Integers.java com.dreamteam.app.rss.MediaAttributes.java com.dreamteam.app.rss.MediaEnclosure.java com.dreamteam.app.rss.MediaThumbnail.java com.dreamteam.app.rss.RSSBase.java com.dreamteam.app.rss.RSSConfig.java com.dreamteam.app.rss.RSSException.java com.dreamteam.app.rss.RSSFault.java com.dreamteam.app.rss.RSSFeed.java com.dreamteam.app.rss.RSSHandler.java com.dreamteam.app.rss.RSSItem.java com.dreamteam.app.rss.RSSLoader.java com.dreamteam.app.rss.RSSParserSPI.java com.dreamteam.app.rss.RSSParser.java com.dreamteam.app.rss.RSSReaderException.java com.dreamteam.app.rss.RSSReader.java com.dreamteam.app.rss.Resources.java com.dreamteam.app.ui.About.java com.dreamteam.app.ui.BaseActivity.java com.dreamteam.app.ui.BaseTitledActivity.java com.dreamteam.app.ui.CategoryDetailActivity.java com.dreamteam.app.ui.ColorListActivity.java com.dreamteam.app.ui.FavoriteItemList.java com.dreamteam.app.ui.FeedCategoryActivity.java com.dreamteam.app.ui.FeedbackUI.java com.dreamteam.app.ui.GuideActivity.java com.dreamteam.app.ui.ImageDialog.java com.dreamteam.app.ui.ImagesBrowseActivity.java com.dreamteam.app.ui.ItemDetailActivity.java com.dreamteam.app.ui.ItemListActivity.java com.dreamteam.app.ui.LocalImageBrowseActivity.java com.dreamteam.app.ui.LoginDialog.java com.dreamteam.app.ui.MainActivity.java com.dreamteam.app.ui.Setting.java com.dreamteam.app.ui.SplashActivity.java com.dreamteam.app.ui.SwitchBgActivity.java com.dreamteam.app.ui.WidgetProvider.java com.dreamteam.app.ui.adapter.ColorListAdapter.java com.dreamteam.app.utils.CategoryNameExchange.java com.dreamteam.app.utils.DateUtils.java com.dreamteam.app.utils.FileUtils.java com.dreamteam.app.utils.HttpUtils.java com.dreamteam.app.utils.ImageLoader.java com.dreamteam.app.utils.ImageUtils.java com.dreamteam.app.utils.Logger.java com.dreamteam.app.utils.MD5.java com.dreamteam.app.utils.StringUtils.java com.dreamteam.app.wallpaper.ChildAdapter.java com.dreamteam.app.wallpaper.GroupGridAdapter.java com.dreamteam.app.wallpaper.ImageBean.java com.dreamteam.app.wallpaper.MyImageView.java com.dreamteam.app.wallpaper.NativeImageLoader.java com.dreamteam.app.wallpaper.WallPaperManager.java com.dreamteam.custom.ui.PathAnimations.java com.dreamteam.custom.ui.PullToRefreshListView.java