수색…


소개

메모리 효율적인 비트 맵 캐싱 : 응용 프로그램에서 애니메이션을 사용하여 GC 정리 중에 중지되고 응용 프로그램이 사용자에게 느린 것처럼 보이는 경우 특히 중요합니다. 캐시를 사용하면 값 비싼 오브젝트를 재사용 할 수 있습니다. 객체를 메모리에로드하면 객체의 캐시로 생각할 수 있습니다. 안드로이드에서 비트 맵 작업은 까다 롭습니다. 반복적으로 사용하려는 경우 bimap을 캐시하는 것이 더 중요합니다.

통사론

  • LruCache<String, Bitmap> mMemoryCache;//declaration of LruCache object.
  • void addBitmapToMemoryCache (String key, Bitmap bitmap) {} // 비트 맵을 캐시 메모리에 추가하는 일반적인 메소드의 선언
  • 비트 맵 getBitmapFromMemCache (String key) {} // 캐시에서 bimap을 가져 오기위한 일반 메소드 선언.

매개 변수

매개 변수 세부
비트 맵을 메모리 캐시에 저장하는 키
비트 맵 메모리에 캐시 할 비트 맵 값

LRU 캐시를 사용하는 비트 맵 캐시

LRU 캐시

다음 예제 코드는 이미지 캐시를위한 LruCache 클래스의 가능한 구현을 보여줍니다.

private LruCache<String, Bitmap> mMemoryCache;

여기서 문자열 값은 비트 맵 값의 핵심입니다.

// 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;
    }
};

비트 맵을 메모리 캐시에 추가하려면

public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }    
}

메모리 캐시에서 비트 맵 가져 오기

public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}

비트 맵을 이미지 뷰로 로드하려면 getBitmapFromMemCache ( "패스 키")를 사용하십시오.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow