am b8a61acb: Bitmapfun Sample: Change cache memory calculation to use maxMemory()

* commit 'b8a61acb072e03665b2c53c7fd049b5bbf5ed1d4':
  Bitmapfun Sample: Change cache memory calculation to use maxMemory()
This commit is contained in:
Adam Koch
2013-01-14 06:15:15 -08:00
committed by Android Git Automerger
5 changed files with 18 additions and 22 deletions

View File

@@ -22,7 +22,7 @@
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

View File

@@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-16
target=android-17

View File

@@ -73,7 +73,7 @@ public class ImageDetailActivity extends FragmentActivity implements OnClickList
ImageCache.ImageCacheParams cacheParams =
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
mImageFetcher = new ImageFetcher(this, longest);

View File

@@ -79,8 +79,7 @@ public class ImageGridFragment extends Fragment implements AdapterView.OnItemCli
ImageCacheParams cacheParams = new ImageCacheParams(getActivity(), IMAGE_CACHE_DIR);
// Set memory cache to 25% of mem class
cacheParams.setMemCacheSizePercent(getActivity(), 0.25f);
cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory
// The ImageFetcher takes care of loading images into our ImageView children asynchronously
mImageFetcher = new ImageFetcher(getActivity(), mImageThumbSize);

View File

@@ -45,10 +45,10 @@ import java.security.NoSuchAlgorithmException;
public class ImageCache {
private static final String TAG = "ImageCache";
// Default memory cache size
private static final int DEFAULT_MEM_CACHE_SIZE = 1024 * 1024 * 5; // 5MB
// Default memory cache size in kilobytes
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
// Compression settings when writing images to disk cache
@@ -128,12 +128,13 @@ public class ImageCache {
}
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
*/
@Override
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.
* Eg. setting percent to 0.2 would set the memory cache to one fifth of the device memory
* class. Throws {@link IllegalArgumentException} if percent is < 0.05 or > .8.
* 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 available
* 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
* Refer to the corresponding Android Training class for more discussion:
* http://developer.android.com/training/displaying-bitmaps/
*
* @param context Context to use to fetch memory class
* @param percent Percent of memory class to use to size memory cache
* @param percent Percent of available app memory 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) {
throw new IllegalArgumentException("setMemCacheSizePercent - percent must be "
+ "between 0.05 and 0.8 (inclusive)");
}
memCacheSize = Math.round(percent * getMemoryClass(context) * 1024 * 1024);
}
private static int getMemoryClass(Context context) {
return ((ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE)).getMemoryClass();
memCacheSize = Math.round(percent * Runtime.getRuntime().maxMemory() / 1024);
}
}