forked from Ramotion/expanding-collection-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented background image caching
- Loading branch information
1 parent
bf3631e
commit 9dcacb3
Showing
21 changed files
with
340 additions
and
78 deletions.
There are no files selected for viewing
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
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
Binary file added
BIN
+130 KB
expanding-collection-simple-example/src/main/res/drawable/attractions.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+56.2 KB
expanding-collection-simple-example/src/main/res/drawable/attractions_head.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+141 KB
expanding-collection-simple-example/src/main/res/drawable/cityscape.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+58.5 KB
expanding-collection-simple-example/src/main/res/drawable/cityscape_head.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+90.1 KB
expanding-collection-simple-example/src/main/res/drawable/cuisine_head.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+69 KB
expanding-collection-simple-example/src/main/res/drawable/nature_head.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+112 KB
expanding-collection-simple-example/src/main/res/drawable/nightlife.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+69.5 KB
expanding-collection-simple-example/src/main/res/drawable/nightlife_head.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions
51
...ding-collection/src/main/java/com/ramotion/expandingcollection/BackgroundBitmapCache.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,51 @@ | ||
package com.ramotion.expandingcollection; | ||
|
||
import android.graphics.Bitmap; | ||
import android.util.LruCache; | ||
|
||
public class BackgroundBitmapCache { | ||
|
||
private final String TAG = "cache_manager"; | ||
|
||
private LruCache<Integer, Bitmap> mBackgroundsCache; | ||
|
||
private static BackgroundBitmapCache instance; | ||
|
||
public static BackgroundBitmapCache getInstance() { | ||
if (instance == null) { | ||
instance = new BackgroundBitmapCache(); | ||
instance.init(); | ||
} | ||
return instance; | ||
} | ||
|
||
private void init() { | ||
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); | ||
|
||
final int cacheSize = maxMemory / 5; | ||
|
||
mBackgroundsCache = new LruCache<Integer, Bitmap>(cacheSize) { | ||
@Override | ||
protected void entryRemoved(boolean evicted, Integer key, Bitmap oldValue, Bitmap newValue) { | ||
super.entryRemoved(evicted, key, oldValue, newValue); | ||
} | ||
|
||
@Override | ||
protected int sizeOf(Integer key, Bitmap bitmap) { | ||
// The cache size will be measured in kilobytes rather than number of items. | ||
return bitmap.getByteCount() / 1024; | ||
} | ||
}; | ||
} | ||
|
||
public void addBitmapToBgMemoryCache(Integer key, Bitmap bitmap) { | ||
if (getBitmapFromBgMemCache(key) == null) { | ||
mBackgroundsCache.put(key, bitmap); | ||
} | ||
} | ||
|
||
public Bitmap getBitmapFromBgMemCache(Integer key) { | ||
return mBackgroundsCache.get(key); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...nding-collection/src/main/java/com/ramotion/expandingcollection/BitmapFactoryOptions.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,9 @@ | ||
package com.ramotion.expandingcollection; | ||
|
||
import android.graphics.BitmapFactory; | ||
|
||
public class BitmapFactoryOptions extends BitmapFactory.Options { | ||
public BitmapFactoryOptions() { | ||
this.inScaled = false; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
expanding-collection/src/main/java/com/ramotion/expandingcollection/BitmapWorkerTask.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,43 @@ | ||
package com.ramotion.expandingcollection; | ||
|
||
import android.content.res.Resources; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.drawable.BitmapDrawable; | ||
import android.os.AsyncTask; | ||
import android.support.annotation.DrawableRes; | ||
|
||
public class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> { | ||
|
||
private final Resources mResources; | ||
private final BackgroundBitmapCache cache; | ||
private final BitmapDrawable mProvidedBitmap; | ||
@DrawableRes | ||
private final int mProvidedBitmapResId; | ||
|
||
public BitmapWorkerTask(Resources resources, BitmapDrawable providedBitmap, @DrawableRes int providedBitmapResId) { | ||
this.mResources = resources; | ||
this.cache = BackgroundBitmapCache.getInstance(); | ||
this.mProvidedBitmap = providedBitmap; | ||
this.mProvidedBitmapResId = providedBitmapResId; | ||
} | ||
|
||
@Override | ||
protected Bitmap doInBackground(Integer... params) { | ||
Integer key = params[0]; | ||
|
||
if (mProvidedBitmap != null && !mProvidedBitmap.getBitmap().isRecycled()) { | ||
cache.addBitmapToBgMemoryCache(key, mProvidedBitmap.getBitmap()); | ||
return mProvidedBitmap.getBitmap(); | ||
} else { | ||
Bitmap cachedBitmap = cache.getBitmapFromBgMemCache(key); | ||
if (cachedBitmap == null) { | ||
cachedBitmap = BitmapFactory.decodeResource(mResources, mProvidedBitmapResId, new BitmapFactoryOptions()); | ||
cache.addBitmapToBgMemoryCache(key, cachedBitmap); | ||
} | ||
return cachedBitmap; | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.