diff --git a/samples/training/bitmapfun/AndroidManifest.xml b/samples/training/bitmapfun/AndroidManifest.xml index cabb442d1..bc1c18057 100644 --- a/samples/training/bitmapfun/AndroidManifest.xml +++ b/samples/training/bitmapfun/AndroidManifest.xml @@ -22,7 +22,7 @@ + android:targetSdkVersion="17" /> diff --git a/samples/training/bitmapfun/project.properties b/samples/training/bitmapfun/project.properties index 9b84a6b4b..a3ee5ab64 100644 --- a/samples/training/bitmapfun/project.properties +++ b/samples/training/bitmapfun/project.properties @@ -11,4 +11,4 @@ #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. -target=android-16 +target=android-17 diff --git a/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageDetailActivity.java b/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageDetailActivity.java index acd483fe8..5fb077656 100644 --- a/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageDetailActivity.java +++ b/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageDetailActivity.java @@ -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); diff --git a/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageGridFragment.java b/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageGridFragment.java index 8a8bcf033..fb18d07f7 100644 --- a/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageGridFragment.java +++ b/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageGridFragment.java @@ -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); diff --git a/samples/training/bitmapfun/src/com/example/android/bitmapfun/util/ImageCache.java b/samples/training/bitmapfun/src/com/example/android/bitmapfun/util/ImageCache.java index 6995eab88..4c4ee2135 100644 --- a/samples/training/bitmapfun/src/com/example/android/bitmapfun/util/ImageCache.java +++ b/samples/training/bitmapfun/src/com/example/android/bitmapfun/util/ImageCache.java @@ -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(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); } }