Java 类android.support.v4.util.LruCache 实例源码

项目:GitHub    文件:Cache.java   
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.");
}
项目:MvpPlus    文件:CustomImageLoaderStrategy.java   
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;
        }
    };
}
项目:qmui    文件:EmojiCache.java   
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();
//              }
            }
        };
    }
项目:chromium-for-android-56-debug-video    文件:ThumbnailProviderImpl.java   
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;
}
项目:Mixtape    文件:PlaylistActivity.java   
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);
}
项目:Mixtape    文件:TestArtworkBinder.java   
/**
 * 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);
}
项目:Mixtape    文件:TestSubtitleBinder.java   
/**
 * 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);
}
项目:Mixtape    文件:TestTitleBinder.java   
/**
 * 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);
}
项目:FreeStreams-TVLauncher    文件:ImageCache.java   
/**
 * 初始化<硬盘缓存>和<内存缓存>
 * @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)
        {
        }
    };
}
项目:QMUI_Android    文件:EmojiCache.java   
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();
//              }
            }
        };
    }
项目:JkImageLoader    文件:JkImageLoader.java   
/**
 * 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();
}
项目:Applozic-Android-Chat-Sample    文件:ImageCache.java   
/**
 * 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;
        }
    };
}
项目:Applozic-Android-Chat-Sample    文件:ImageCache.java   
/**
 * 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;
        }
    };
}
项目:libcommon    文件:MediaStoreAdapter.java   
/**
 * 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();
}
项目:GalleryPictureFirebaseAndroid    文件:Utils.java   
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;
        }
    };
}
项目:LittleFreshWeather    文件:MemoryCacheManager.java   
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();
        }
    };
}
项目:talk-android    文件:BaseRecipientAdapter.java   
public BaseRecipientAdapter(Context context, int preferredMaxResultCount, int queryMode) {
    mContext = context;
    mContentResolver = context.getContentResolver();
    mInflater = LayoutInflater.from(context);
    mPreferredMaxResultCount = preferredMaxResultCount;
    if (mPhotoCacheMap == null) {
        mPhotoCacheMap = new LruCache<Uri, byte[]>(PHOTO_CACHE_SIZE);
    }
    mQueryType = queryMode;

    if (queryMode == QUERY_TYPE_EMAIL) {
        mQuery = Queries.EMAIL;
    } else if (queryMode == QUERY_TYPE_PHONE) {
        mQuery = Queries.PHONE;
    } else {
        mQuery = Queries.EMAIL;
        Log.e(TAG, "Unsupported query type: " + queryMode);
    }
}
项目:dontsuck-mp    文件:BitmapCache.java   
@Inject BitmapCache() {
  // 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;

  cache = new LruCache<Uri, Bitmap>(cacheSize) {
    @Override protected int sizeOf(Uri key, Bitmap bitmap) {
      // The cache size will be measured in kilobytes rather than
      // number of items.
      return bitmap.getByteCount() / 1024;
    }
  };
}
项目:EpicBitmapRenderer    文件:EpicBitmapCache.java   
/**
 * Basic constructor, builds the memory cache automatically.
 */
public EpicBitmapCache() {
    // 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;

    //Initialize the memory cache
    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.getRowBytes() * bitmap.getHeight()) / 1024;
        }
    };
}
项目:Accessories_Android    文件:MemCache.java   
public MemCache(CachePolicy cachePolicy) {
    int poolSize = cachePolicy.getMemCachePoolSize();
    mLruCache = new LruCache<String, Bitmap>(poolSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            if (value == null) return 0;
            return value.getWidth() * value.getHeight();
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
            super.entryRemoved(evicted, key, oldValue, newValue);
            // Hard to recycle.
            oldValue = null;
        }
    };
}
项目:LLApp    文件:ImageLoader.java   
/**
 * 初始化
 * 
 * @param threadCount
 * @param type
 */
private void init(int threadCount, Type type,Context context)
{
    // 获取我们应用的最大可用内存
    int maxMemory = (int) Runtime.getRuntime().maxMemory();
    int cacheMemory = maxMemory / 8;
    //注意此处要获取全局Context,避免引用Activity造成资源无法释放
    mContext=context.getApplicationContext();
    mLruCache = new LruCache<String, Bitmap>(cacheMemory){
        @Override
        protected int sizeOf(String key, Bitmap value)
        {
            //              return value.getAllocationByteCount();
            return value.getRowBytes() * value.getHeight(); //旧版本方法
        }

    };

    // 创建线程池
    mThreadPool = Executors.newFixedThreadPool(threadCount);
    mType = type;
    mSemaphoreThreadPool = new Semaphore(threadCount,true);
    mTaskQueue = new LinkedBlockingDeque<Runnable>();   
    initBackThread();
}
项目:Bitutorial    文件:TransformationAdapterWrapper.java   
private TransformationAdapterWrapper(@NonNull Builder builder) {
    this.inflater = LayoutInflater.from(builder.context);
    this.innerAdapter = builder.innerAdapter;
    this.rows = builder.rows;
    this.columns = builder.columns;
    this.marginTop = builder.marginTop;
    this.imagesCache = new LruCache<>(OFFSET_PAGES);
    this.translationX = builder.translationX;
    this.translationY = builder.translationY;
    this.piecesSpacing = builder.piecesSpacing;
    this.complexViewDetector = builder.complexViewDetector;
    this.bitmapTransformerFactory = builder.bitmapTransformerFactory;
    this.bitmapScale = builder.bitmapScale;
    this.itemsMap = new HashMap<>();
    this.positionsMap = new HashMap<>();
}
项目:AndroidChromium    文件:ThumbnailProviderImpl.java   
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;
}
项目:GanWuMei    文件:ImageLoader.java   
/**
 * 初始化
 *
 * @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();
        }

    };

    // 创建线程池
    mThreadPool = Executors.newFixedThreadPool(threadCount);
    mTaskQueue = new LinkedList<Runnable>();
    mType = type;
    mSemaphoreThreadPool = new Semaphore(threadCount);
}
项目:odyssey    文件:CurrentPlaylistAdapter.java   
public CurrentPlaylistAdapter(Context context, PlaybackServiceConnection playbackServiceConnection) {
    super();

    mContext = context;


    try {
        mPBS = playbackServiceConnection.getPBS();
        mPlaylistSize = mPBS.getPlaylistSize();
        mCurrentPlayingIndex = mPBS.getCurrentIndex();
    } catch (RemoteException e) {
        mPBS = null;
        e.printStackTrace();
    }

    mArtworkManager = ArtworkManager.getInstance(context.getApplicationContext());
    mTrackCache = new LruCache<>(CACHE_SIZE);
}
项目:Practice    文件:SDCardImageLoader.java   
public SDCardImageLoader(int screenW, int screenH) {
    this.screenW = screenW;
    this.screenH = screenH;

    // 获取应用程序最大可用内存
    int maxMemory = (int) Runtime.getRuntime().maxMemory();
    int cacheSize = maxMemory / 8;

    // 设置图片缓存大小为程序最大可用内存的1/8
    imageCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };
}
项目:RxList    文件:MapList2.java   
private MapList2(List<T> list, List<T2> list2, Mappable2<T, T2, E> mapper, ListMappable2<T, T2, E> indexMapper) {
    super();

    mList = Collections.emptyList();
    if (list != null) {
        mList = list;
    }
    mList2 = Collections.emptyList();
    if (list2 != null) {
        mList2 = list2;
    }
    mMapper = mapper == null ? this : mapper;
    mIndexMapper = indexMapper == null ? this : indexMapper;
    mFilter = this;
    mFilter2 = this;
    mCache = new LruCache<Integer, E>(1000);
}
项目:android-founder-directory    文件:BitmapWorkerTask.java   
public BitmapWorkerTask(ImageView imageView) {
    // Use a WeakReference to ensure the ImageView can be garbage collected
    if (cache == null) {
        final int memClass = ((ActivityManager) imageView.getContext().getSystemService(
                Context.ACTIVITY_SERVICE)).getMemoryClass();
        cache = new LruCache<String, Bitmap>(1024 * 1024 * memClass / 3) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }

    if (diskCache == null) {
        try {
            diskCache = DiskLruCache.open(imageView.getContext().getCacheDir(), 1, 1, 1024 * 1024 * 10);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    weakReference = new WeakReference<>(imageView);
}
项目:TheSceneryAlong    文件:ThumbnailLoader.java   
private ThumbnailLoader(){
    memoryCache = new LruCache<String, Bitmap>(2 * FinalConstants.byte_1m){
        @Override
        protected int sizeOf(String key, Bitmap value) {

            return value.getWidth() * value.getHeight() * 4;
        }

        @Override
        protected void entryRemoved(boolean evicted, String key,
                Bitmap oldValue, Bitmap newValue) {

            super.entryRemoved(evicted, key, oldValue, newValue);
        }
    };

    diskCache = DiskLruCache.openCache(App.app, 
            new File(PathConstants.getThumbpath()), 
            16 * FinalConstants.byte_1m);
}
项目:lr_dialer    文件:BaseContactsAdapter.java   
public BaseContactsAdapter(Context context, List<T> list)
{
    super();
    this.context = context;
    this.list = list;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize)
    {
        @Override
        protected int sizeOf(String key, Bitmap bitmap)
        {
            if (AppFactory.isCompatible(12))
            {
                return bitmap.getByteCount() / 1024;
            }
            else
            {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };
}
项目:lr_dialer    文件:SearchAdapter.java   
public SearchAdapter(Context context, List<CallLogInfo> callLogInfoList, List<ContactInfo> contactInfoList)
{
    this.context = context;
    this.callLogInfoList = callLogInfoList;
    this.contactInfoList = contactInfoList;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize)
    {
        @Override
        protected int sizeOf(String key, Bitmap bitmap)
        {
            if (AppFactory.isCompatible(12))
            {
                return bitmap.getByteCount() / 1024;
            }
            else
            {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };
}
项目:Rocket.Chat-android    文件:CollectionDAO.java   
public static List<CollectionDAO> query(String collectionName) {
    List<CollectionDAO> list = new ArrayList<>();
    LruCache<String, CollectionDAO> cache = mCache.get(collectionName);
    if (cache != null && cache.size() > 0) {
        list.addAll(cache.snapshot().values());
        return list;
    }

    Cursor cursor = DBManager.getInstance().query(TABLE_NAME, null, COLUMN_COLLECTION_NAME + "=?", new String[]{collectionName});
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {
                list.add(new CollectionDAO(cursor));
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
    return list;
}
项目:Rocket.Chat-android    文件:CollectionDAO.java   
public static CollectionDAO query(String collectionName, String documentID) {
    CollectionDAO dao = null;
    LruCache<String, CollectionDAO> cache = mCache.get(collectionName);
    if (cache != null) {
        dao = cache.get(documentID);
        if (dao != null) {
            return dao;
        }
    }

    Cursor cursor = DBManager.getInstance().query(TABLE_NAME, null, COLUMN_COLLECTION_NAME + "=? AND " + COLUMN_DOCUMENT_ID + "=?", new String[]{collectionName, documentID});
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {
                dao = new CollectionDAO(cursor);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
    return dao;
}
项目:margarita    文件:ImageCache.java   
/**
 * 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;
        }
    };
}
项目:clear-todolist    文件:Cache.java   
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.");
}
项目:android-tv-launcher    文件:ImageCache.java   
/**
 * 初始化<硬盘缓存>和<内存缓存>
 * @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)
        {
        }
    };
}
项目:vlc_android_win    文件:BitmapCache.java   
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private BitmapCache() {

    // Get memory class of this device, exceeding this amount will throw an
    // OutOfMemory exception.
    final ActivityManager am = ((ActivityManager) VLCApplication.getAppContext().getSystemService(
            Context.ACTIVITY_SERVICE));
    final int memClass = AndroidUtil.isHoneycombOrLater() ? am.getLargeMemoryClass() : am.getMemoryClass();

    // Use 1/5th of the available memory for this memory cache.
    final int cacheSize = 1024 * 1024 * memClass / 5;

    Log.d(TAG, "LRUCache size sets to " + cacheSize);

    mMemCache = new LruCache<String, Bitmap>(cacheSize) {

        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }

    };
}
项目:Applozic-Android-SDK    文件:ImageCache.java   
/**
 * 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;
        }
    };
}
项目:World-Weather    文件:RefreshingActivity.java   
/**
 * Obtains or creates a new memory cache to store the weather icons.
 */
private void setIconMemoryCache() {
    IconCacheRetainFragment retainFragment =
            IconCacheRetainFragment.findOrCreateRetainFragment(getSupportFragmentManager());
    iconCache = retainFragment.iconCache;
    if (iconCache == null) {
        // maximum available VM memory, stored in kilobytes
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        // we use 1/8th of the available memory for this memory cache
        int cacheSize = maxMemory / 8;

        iconCache = 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;
            }
        };

    }
    retainFragment.iconCache = iconCache;
}
项目:Translation    文件:NativeImageLoader.java   
private NativeImageLoader()
{
    //获取应用程序的最大内存
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    //用最大内存的1/4来存储图片
    final int cacheSize = maxMemory / 4;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize)
    {

        //获取每张图片的大小
        @Override
        protected int sizeOf(String key, Bitmap bitmap)
        {
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }
    };
}