android.support.v4.util.LruCache Java Examples

The following examples show how to use android.support.v4.util.LruCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BitmapMemoryCache.java    From KJFrameForAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * @param maxSize 使用内存缓存的内存大小,单位:kb
 */
@SuppressLint("NewApi")
private void init(int maxSize) {
    this.maxSize = maxSize;
    cache = new LruCache<String, Bitmap>(maxSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            super.sizeOf(key, value);
            if (SystemTool.getSDKVersion() >= 12) {
                return value.getByteCount();
            } else {
                return value.getRowBytes() * value.getHeight();
            }
        }
    };
}
 
Example #2
Source File: ImageLoader.java    From android-art-res with Apache License 2.0 6 votes vote down vote up
private ImageLoader(Context context) {
    mContext = context.getApplicationContext();
    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;
        }
    };
    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();
        }
    }
}
 
Example #3
Source File: ImagesCache.java    From StreamHub-Android-SDK with MIT License 6 votes vote down vote up
public void initializeCache()
{
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() /1024);

    final int cacheSize = maxMemory / 8;

    System.out.println("cache size = "+cacheSize);

    imagesWarehouse = new LruCache<String, Bitmap>(cacheSize)
            {
                protected int sizeOf(String key, Bitmap value) 
                {
                    // The cache size will be measured in kilobytes rather than number of items.

                    int bitmapByteCount = value.getRowBytes() * value.getHeight();

                    return bitmapByteCount / 1024;
                }
            };
}
 
Example #4
Source File: GridViewActivity.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
public MyAdapter(Context context, List<Integer> list) {
	super(list);
	mContext = context;
	final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

	// Use 1/8th of the available memory for this memory cache.
	final int cacheSize = maxMemory;
	mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
		@Override
		protected int sizeOf(Integer key, Bitmap bitmap) {
			// The cache size will be measured in kilobytes rather than
			// number of items.
			return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
		}
	};
}
 
Example #5
Source File: VolleyImageCacheImpl.java    From seny-devpkg with Apache License 2.0 6 votes vote down vote up
public VolleyImageCacheImpl(Context context) {
    // 获取应用可占内存的1/8作为缓存
    int maxSize = (int) (Runtime.getRuntime().maxMemory() / 8);
    // 实例化LruCache对象
    mLruCache = new LruCache<String, Bitmap>(maxSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            //测量bitmap的大小
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    };
    try {
        // 获取DiskLruCahce对象
        mDiskLruCache = DiskLruCache.open(getDiskCacheDir(context, "bitmaps"), getAppVersion(context), 1, DISK_MAX_SIZE);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #6
Source File: MemoryCache.java    From simple_imageloader with MIT License 6 votes vote down vote up
public MemoryCache() {

        // 计算可使用的最大内存
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        // 取4分之一的可用内存作为缓存
        final int cacheSize = maxMemory / 4;
        mMemeryCache = new LruCache<String, Bitmap>(cacheSize) {

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

    }
 
Example #7
Source File: ImageCache.java    From ListViewVariants with Apache License 2.0 6 votes vote down vote up
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)
{
@Override
protected int sizeOf(String key,Bitmap bitmap)
  {
  final int bitmapSize=getBitmapSize(bitmap)/1024;
  return bitmapSize==0?1:bitmapSize;
  }
};
}
 
Example #8
Source File: BaseRecipientAdapter.java    From talk-android with MIT License 6 votes vote down vote up
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);
    }
}
 
Example #9
Source File: ImageLoader.java    From LLApp with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化
 * 
 * @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();
}
 
Example #10
Source File: ImageCache.java    From Yahala-Messenger with MIT License 6 votes vote down vote up
/**
 * 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;
        }
    };
}
 
Example #11
Source File: CoverLoader.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
private CoverLoader() {
    // 获取当前进程的可用内存(单位KB)
    int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    // 缓存大小为当前进程可用内存的1/8
    int cacheSize = maxMemory / 8;
    mCoverCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            //API19
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                return bitmap.getAllocationByteCount() / 1024;
            } else {
                return bitmap.getByteCount() / 1024;
            }
        }
    };
}
 
Example #12
Source File: ImageLoader.java    From AntennaPodSP with MIT License 6 votes vote down vote up
private ImageLoader() {
    handler = new Handler();
    executor = createExecutor();

    coverCache = new LruCache<String, CachedBitmap>(1);

    thumbnailCache = new LruCache<String, CachedBitmap>(thumbnailCacheSize) {

        @SuppressLint("NewApi")
        @Override
        protected int sizeOf(String key, CachedBitmap value) {
            if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
                return value.getBitmap().getByteCount();
            else
                return (value.getBitmap().getRowBytes() * value.getBitmap()
                        .getHeight());

        }

    };
}
 
Example #13
Source File: ImageCache.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
private  ImageCache(){
    //初始化lruCache
    //计算最大内存
    long maxMemory = Runtime.getRuntime().maxMemory();
    Log.d("Tag","maxMemory:"+maxMemory);
    //设置缓存为最大内存的1/8
    lruCache = new LruCache<String,Bitmap>((int) (maxMemory/8)){

        @Override
        protected int sizeOf(String key, Bitmap value) {
            //计算bimap的大小
            int count = value.getByteCount();
            return count;
        }
    };
}
 
Example #14
Source File: ViewsStateBundle.java    From adt-leanback-support with Apache License 2.0 6 votes vote down vote up
protected void applyPolicyChanges() {
    if (mSavePolicy == SAVE_LIMITED_CHILD) {
        if (mLimitNumber <= 0) {
            throw new IllegalArgumentException();
        }
        if (mChildStates == null || mChildStates.maxSize() != mLimitNumber) {
            mChildStates = new LruCache<String, SparseArray<Parcelable>>(mLimitNumber);
        }
    } else if (mSavePolicy == SAVE_ALL_CHILD || mSavePolicy == SAVE_ON_SCREEN_CHILD) {
        if (mChildStates == null || mChildStates.maxSize() != UNLIMITED) {
            mChildStates = new LruCache<String, SparseArray<Parcelable>>(UNLIMITED);
        }
    } else {
        mChildStates = null;
    }
}
 
Example #15
Source File: MemoryCacheManager.java    From LittleFreshWeather with Apache License 2.0 6 votes vote down vote up
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();
        }
    };
}
 
Example #16
Source File: MusicIconLoader.java    From LitePlayer with Apache License 2.0 5 votes vote down vote up
private MusicIconLoader() {
		int maxSize = (int) (Runtime.getRuntime().maxMemory() / 8);
		mCache = new LruCache<String, Bitmap>(maxSize) {
			protected int sizeOf(String key, Bitmap value) {
//				return value.getByteCount();
				return value.getRowBytes() * value.getHeight();
			}
		};
	}
 
Example #17
Source File: BitmapCache.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Конструктор
 * @param maxSize максимальный размер кэша в памяти в байтах
 * @param fileCache объект файлового кэша
 */
public BitmapCache(int maxSize, FileCache fileCache) {
    this.fileCache = fileCache;
    this.lru = new LruCache<String, Bitmap>(maxSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };
    this.currentDownloads = new HashSet<String>();
}
 
Example #18
Source File: RssListAdapter.java    From android-opensource-library-56 with Apache License 2.0 5 votes vote down vote up
public LruImageCache() {
    // 5MBのキャッシュ
    int cacheSize = 5 * 1024 * 1024;
    mCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };
}
 
Example #19
Source File: GalleryCache.java    From MediaChooser with Apache License 2.0 5 votes vote down vote up
public GalleryCache(int size, int maxWidth, int maxHeight) {
    mMaxWidth = maxWidth;

    mBitmapCache = new LruCache<String, Bitmap>(size) {
        @Override
        protected int sizeOf(String key, Bitmap b) {
            // Assuming that one pixel contains four bytes.
            return b.getHeight() * b.getWidth() * 4;
        }
    };

    mCurrentTasks = new ArrayList<String>();
}
 
Example #20
Source File: MyVideoThumbLoader.java    From KSYMediaPlayer_Android with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
public MyVideoThumbLoader(){
    int maxMemory = (int) Runtime.getRuntime().maxMemory();//获取最大的运行内存
    int maxSize = maxMemory /4;//拿到缓存的内存大小 35         lruCache = new LruCache<String, Bitmap>(maxSize){

    lruCache = new LruCache<String, Bitmap>(maxSize){
        @Override
        protected int sizeOf(String key, Bitmap value) {
            //这个方法会在每次存入缓存的时候调用
            return value.getByteCount();
        }
    };
}
 
Example #21
Source File: TileCache.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public TileCache( final Context context ) {
	try {
		digest = MessageDigest.getInstance( "MD5" );
	} catch ( NoSuchAlgorithmException e1 ) {
		
	}
	// in memory cache
	final int memory = (int) ( Runtime.getRuntime().maxMemory() / 1024 );
	final int size = memory / 8;
	memoryCache = new LruCache<String, Bitmap>( size ) {
		@Override
		protected int sizeOf( String key, Bitmap bitmap ) {
			// The cache size will be measured in kilobytes rather than number of items.
			// emulate bitmap.getByteCount for APIs less than 12
			int byteCount = bitmap.getRowBytes() * bitmap.getHeight();
			return byteCount / 1024;
		}
	};
	// disk cache
	new Thread(new Runnable(){
		@Override
		public void run(){
			File cacheDirectory = new File( context.getCacheDir().getPath() + File.separator + "com/qozix/mapview" );
			try {
				diskCache = DiskLruCache.open( cacheDirectory, 1, 1, DISK_CACHE_CAPACITY );
			} catch ( IOException e ) {
				
			}
		}
	}).start();
}
 
Example #22
Source File: CacheImage.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
public CacheImage()
{
	this.mCache = new LruCache<String, Bitmap>(MAX_CACHE_SIZE)
	{

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

	};
}
 
Example #23
Source File: UIImageLoaderActivity.java    From JCVideoPlayer with MIT License 5 votes vote down vote up
public BitmapCache() {
    cache = new LruCache<String, Bitmap>(8 * 1024 * 1024) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    };
}
 
Example #24
Source File: LocalImageLoader.java    From ImageChooser with Apache License 2.0 5 votes vote down vote up
private LocalImageLoader() {
    // 获取应用程序的最大内存
    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;
        }
    };
}
 
Example #25
Source File: MyVideoThumbLoader.java    From ksyhttpcache_android with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
public MyVideoThumbLoader(){
    int maxMemory = (int) Runtime.getRuntime().maxMemory();//获取最大的运行内存
    int maxSize = maxMemory /4;//拿到缓存的内存大小 35         lruCache = new LruCache<String, Bitmap>(maxSize){

    lruCache = new LruCache<String, Bitmap>(maxSize){
        @Override
        protected int sizeOf(String key, Bitmap value) {
            //这个方法会在每次存入缓存的时候调用
            return value.getByteCount();
        }
    };
}
 
Example #26
Source File: BitmapCache.java    From BlogDemo with Apache License 2.0 5 votes vote down vote up
public BitmapCache() {
    //初始化 LruCache
    lruCache = new LruCache<String, Bitmap>(max) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };
}
 
Example #27
Source File: ImageLoader.java    From base-imageloader with Apache License 2.0 5 votes vote down vote up
private LruCache createDefaultLruCache()
{
    int memCacheSize = mMaxMemCacheSize;
    int maxMemory = (int) Runtime.getRuntime().maxMemory();
    memCacheSize = memCacheSize <= 0 ? maxMemory / 8 : memCacheSize;
    return new LruCache<String, Bitmap>(memCacheSize)
    {
        @Override
        protected int sizeOf(String key, Bitmap value)
        {
            return value.getRowBytes() * value.getHeight();
        }
    };
}
 
Example #28
Source File: ExpandableListItemActivity.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ExpandableListItemAdapter with the specified list, or an empty list if
 * items == null.
 */
private MyExpandableListItemAdapter(final Context context, final List<Integer> items) {
    super(context, R.layout.list_anim_activity_expandablelistitem_card, R.id.activity_expandablelistitem_card_title, R.id.activity_expandablelistitem_card_content, items);
    mContext = context;

    final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
    mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(final Integer key, final Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }
    };
}
 
Example #29
Source File: LruMemCache.java    From simple_net_framework with MIT License 5 votes vote down vote up
public LruMemCache() {
    // 计算可使用的最大内存
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // 取八分之一的可用内存作为缓存
    final int cacheSize = maxMemory / 8;
    mResponseCache = new LruCache<String, Response>(cacheSize) {

        @Override
        protected int sizeOf(String key, Response response) {
            return response.rawData.length / 1024;
        }
    };

}
 
Example #30
Source File: ImageLoader.java    From Popeens-DSub with GNU General Public License v3.0 5 votes vote down vote up
public ImageLoader(Context context) {
	this.context = context;
	handler = new Handler(Looper.getMainLooper());
	final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
	cacheSize = maxMemory / 4;

	// Determine the density-dependent image sizes.
	imageSizeDefault = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
	DisplayMetrics metrics = context.getResources().getDisplayMetrics();
	imageSizeLarge = Math.round(Math.min(metrics.widthPixels, metrics.heightPixels));
	avatarSizeDefault = context.getResources().getDrawable(R.drawable.ic_social_person).getIntrinsicHeight();

	cache = new LruCache<String, Bitmap>(cacheSize) {
		@Override
		protected int sizeOf(String key, Bitmap bitmap) {
			return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
		}

		@Override
		protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
			if(evicted) {
				if((oldBitmap != nowPlaying && oldBitmap != nowPlayingSmall) || clearingCache) {
					oldBitmap.recycle();
				} else if(oldBitmap != newBitmap) {
					cache.put(key, oldBitmap);
				}
			}
		}
	};
}