This repository has been archived by the owner on Oct 15, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start refactoring code so that extra levels of caching can be adding.…
… BitmapLruCache is now a wrapper around other caches, and is created with a Builder.
- Loading branch information
Chris Banes
committed
Dec 15, 2012
1 parent
a2fa54d
commit 85844ca
Showing
7 changed files
with
134 additions
and
411 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
library/src/uk/co/senab/bitmapcache/BitmapMemoryLruCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/******************************************************************************* | ||
* Copyright 2011, 2012 Chris Banes. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package uk.co.senab.bitmapcache; | ||
|
||
import java.util.Map.Entry; | ||
|
||
import android.graphics.Bitmap; | ||
import android.support.v4.util.LruCache; | ||
|
||
final class BitmapMemoryLruCache extends LruCache<String, CacheableBitmapWrapper> { | ||
|
||
static final String LOG_TAG = "BitmapMemoryLruCache"; | ||
|
||
BitmapMemoryLruCache(int maxSize) { | ||
super(maxSize); | ||
} | ||
|
||
@Override | ||
protected int sizeOf(String key, CacheableBitmapWrapper value) { | ||
if (value.hasValidBitmap()) { | ||
Bitmap bitmap = value.getBitmap(); | ||
return bitmap.getRowBytes() * bitmap.getHeight(); | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
protected void entryRemoved(boolean evicted, String key, CacheableBitmapWrapper oldValue, | ||
CacheableBitmapWrapper newValue) { | ||
// Notify the wrapper that it's no longer being cached | ||
oldValue.setCached(false); | ||
} | ||
|
||
void trimMemory() { | ||
for (Entry<String, CacheableBitmapWrapper> entry : snapshot().entrySet()) { | ||
CacheableBitmapWrapper value = entry.getValue(); | ||
if (null == value || !value.isBeingDisplayed()) { | ||
remove(entry.getKey()); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.