* commit 'ec5a8473253c4458b996cbfba6ed8b0bbb82ef5f': Bitmapfun Sample: Change cache memory calculation to use maxMemory()
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
<uses-sdk
|
<uses-sdk
|
||||||
android:minSdkVersion="7"
|
android:minSdkVersion="7"
|
||||||
android:targetSdkVersion="16" />
|
android:targetSdkVersion="17" />
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
|||||||
@@ -11,4 +11,4 @@
|
|||||||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
|
||||||
|
|
||||||
# Project target.
|
# Project target.
|
||||||
target=android-16
|
target=android-17
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ public class ImageDetailActivity extends FragmentActivity implements OnClickList
|
|||||||
|
|
||||||
ImageCache.ImageCacheParams cacheParams =
|
ImageCache.ImageCacheParams cacheParams =
|
||||||
new ImageCache.ImageCacheParams(this, IMAGE_CACHE_DIR);
|
new ImageCache.ImageCacheParams(this, IMAGE_CACHE_DIR);
|
||||||
cacheParams.setMemCacheSizePercent(this, 0.25f); // Set memory cache to 25% of mem class
|
cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory
|
||||||
|
|
||||||
// The ImageFetcher takes care of loading images into our ImageView children asynchronously
|
// The ImageFetcher takes care of loading images into our ImageView children asynchronously
|
||||||
mImageFetcher = new ImageFetcher(this, longest);
|
mImageFetcher = new ImageFetcher(this, longest);
|
||||||
|
|||||||
@@ -79,8 +79,7 @@ public class ImageGridFragment extends Fragment implements AdapterView.OnItemCli
|
|||||||
|
|
||||||
ImageCacheParams cacheParams = new ImageCacheParams(getActivity(), IMAGE_CACHE_DIR);
|
ImageCacheParams cacheParams = new ImageCacheParams(getActivity(), IMAGE_CACHE_DIR);
|
||||||
|
|
||||||
// Set memory cache to 25% of mem class
|
cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory
|
||||||
cacheParams.setMemCacheSizePercent(getActivity(), 0.25f);
|
|
||||||
|
|
||||||
// The ImageFetcher takes care of loading images into our ImageView children asynchronously
|
// The ImageFetcher takes care of loading images into our ImageView children asynchronously
|
||||||
mImageFetcher = new ImageFetcher(getActivity(), mImageThumbSize);
|
mImageFetcher = new ImageFetcher(getActivity(), mImageThumbSize);
|
||||||
|
|||||||
@@ -45,10 +45,10 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
public class ImageCache {
|
public class ImageCache {
|
||||||
private static final String TAG = "ImageCache";
|
private static final String TAG = "ImageCache";
|
||||||
|
|
||||||
// Default memory cache size
|
// Default memory cache size in kilobytes
|
||||||
private static final int DEFAULT_MEM_CACHE_SIZE = 1024 * 1024 * 5; // 5MB
|
private static final int DEFAULT_MEM_CACHE_SIZE = 1024 * 5; // 5MB
|
||||||
|
|
||||||
// Default disk cache size
|
// Default disk cache size in bytes
|
||||||
private static final int DEFAULT_DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
|
private static final int DEFAULT_DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
|
||||||
|
|
||||||
// Compression settings when writing images to disk cache
|
// Compression settings when writing images to disk cache
|
||||||
@@ -128,12 +128,13 @@ public class ImageCache {
|
|||||||
}
|
}
|
||||||
mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
|
mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
|
||||||
/**
|
/**
|
||||||
* Measure item size in bytes rather than units which is more practical
|
* Measure item size in kilobytes rather than units which is more practical
|
||||||
* for a bitmap cache
|
* for a bitmap cache
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected int sizeOf(String key, Bitmap bitmap) {
|
protected int sizeOf(String key, Bitmap bitmap) {
|
||||||
return getBitmapSize(bitmap);
|
final int bitmapSize = getBitmapSize(bitmap) / 1024;
|
||||||
|
return bitmapSize == 0 ? 1 : bitmapSize;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -383,28 +384,24 @@ public class ImageCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the memory cache size based on a percentage of the device memory class.
|
* Sets the memory cache size based on a percentage of the max available VM memory.
|
||||||
* Eg. setting percent to 0.2 would set the memory cache to one fifth of the device memory
|
* Eg. setting percent to 0.2 would set the memory cache to one fifth of the available
|
||||||
* class. Throws {@link IllegalArgumentException} if percent is < 0.05 or > .8.
|
* memory. Throws {@link IllegalArgumentException} if percent is < 0.05 or > .8.
|
||||||
|
* memCacheSize is stored in kilobytes instead of bytes as this will eventually be passed
|
||||||
|
* to construct a LruCache which takes an int in its constructor.
|
||||||
*
|
*
|
||||||
* This value should be chosen carefully based on a number of factors
|
* This value should be chosen carefully based on a number of factors
|
||||||
* Refer to the corresponding Android Training class for more discussion:
|
* Refer to the corresponding Android Training class for more discussion:
|
||||||
* http://developer.android.com/training/displaying-bitmaps/
|
* http://developer.android.com/training/displaying-bitmaps/
|
||||||
*
|
*
|
||||||
* @param context Context to use to fetch memory class
|
* @param percent Percent of available app memory to use to size memory cache
|
||||||
* @param percent Percent of memory class to use to size memory cache
|
|
||||||
*/
|
*/
|
||||||
public void setMemCacheSizePercent(Context context, float percent) {
|
public void setMemCacheSizePercent(float percent) {
|
||||||
if (percent < 0.05f || percent > 0.8f) {
|
if (percent < 0.05f || percent > 0.8f) {
|
||||||
throw new IllegalArgumentException("setMemCacheSizePercent - percent must be "
|
throw new IllegalArgumentException("setMemCacheSizePercent - percent must be "
|
||||||
+ "between 0.05 and 0.8 (inclusive)");
|
+ "between 0.05 and 0.8 (inclusive)");
|
||||||
}
|
}
|
||||||
memCacheSize = Math.round(percent * getMemoryClass(context) * 1024 * 1024);
|
memCacheSize = Math.round(percent * Runtime.getRuntime().maxMemory() / 1024);
|
||||||
}
|
|
||||||
|
|
||||||
private static int getMemoryClass(Context context) {
|
|
||||||
return ((ActivityManager) context.getSystemService(
|
|
||||||
Context.ACTIVITY_SERVICE)).getMemoryClass();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user