summaryrefslogtreecommitdiffstats
path: root/docs/html/training/displaying-bitmaps/manage-memory.jd
diff options
context:
space:
mode:
Diffstat (limited to 'docs/html/training/displaying-bitmaps/manage-memory.jd')
-rw-r--r--docs/html/training/displaying-bitmaps/manage-memory.jd60
1 files changed, 37 insertions, 23 deletions
diff --git a/docs/html/training/displaying-bitmaps/manage-memory.jd b/docs/html/training/displaying-bitmaps/manage-memory.jd
index 60ac2e6..0e1279e 100644
--- a/docs/html/training/displaying-bitmaps/manage-memory.jd
+++ b/docs/html/training/displaying-bitmaps/manage-memory.jd
@@ -56,7 +56,7 @@ bitmap is stored in native memory. It is separate from the bitmap itself,
which is stored in the Dalvik heap. The pixel data in native memory is
not released in a predictable manner, potentially causing an application
to briefly exceed its memory limits and crash.
-<strong>As of Android 3.0 (API Level 11), the pixel data is stored on the
+<strong>As of Android 3.0 (API level 11), the pixel data is stored on the
Dalvik heap along with the associated bitmap.</strong></li>
</ul>
@@ -140,27 +140,16 @@ private synchronized boolean hasValidBitmap() {
<h2 id="inBitmap">Manage Memory on Android 3.0 and Higher</h2>
-<p>Android 3.0 (API Level 11) introduces the
+<p>Android 3.0 (API level 11) introduces the
{@link android.graphics.BitmapFactory.Options#inBitmap BitmapFactory.Options.inBitmap}
field. If this option is set, decode methods that take the
{@link android.graphics.BitmapFactory.Options Options} object
will attempt to reuse an existing bitmap when loading content. This means
that the bitmap's memory is reused, resulting in improved performance, and
-removing both memory allocation and de-allocation. There are some caveats in using
-{@link android.graphics.BitmapFactory.Options#inBitmap}:</p>
-<ul>
- <li>The reused bitmap must be of the same size as the source content (to make
-sure that the same amount of memory is used), and in JPEG or PNG format
-(whether as a resource or as a stream).</li>
-
-
-<li>The {@link android.graphics.Bitmap.Config configuration} of the reused bitmap
-overrides the setting of
-{@link android.graphics.BitmapFactory.Options#inPreferredConfig}, if set. </li>
-
- <li>You should always use the returned bitmap of the decode method,
-because you can't assume that reusing the bitmap worked (for example, if there is
-a size mismatch).</li>
+removing both memory allocation and de-allocation. However, there are certain restrictions with how
+{@link android.graphics.BitmapFactory.Options#inBitmap} can be used. In particular, before Android
+4.4 (API level 19), only equal sized bitmaps are supported. For details, please see the
+{@link android.graphics.BitmapFactory.Options#inBitmap} documentation.
<h3>Save a bitmap for later use</h3>
@@ -283,14 +272,39 @@ protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
satisfies the size criteria to be used for
{@link android.graphics.BitmapFactory.Options#inBitmap}:</p>
-<pre>private static boolean canUseForInBitmap(
+<pre>static boolean canUseForInBitmap(
Bitmap candidate, BitmapFactory.Options targetOptions) {
- int width = targetOptions.outWidth / targetOptions.inSampleSize;
- int height = targetOptions.outHeight / targetOptions.inSampleSize;
- // Returns true if "candidate" can be used for inBitmap re-use with
- // "targetOptions".
- return candidate.getWidth() == width && candidate.getHeight() == height;
+ if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) {
+ // From Android 4.4 (KitKat) onward we can re-use if the byte size of
+ // the new bitmap is smaller than the reusable bitmap candidate
+ // allocation byte count.
+ int width = targetOptions.outWidth / targetOptions.inSampleSize;
+ int height = targetOptions.outHeight / targetOptions.inSampleSize;
+ int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
+ return byteCount &lt;= candidate.getAllocationByteCount();
+ }
+
+ // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
+ return candidate.getWidth() == targetOptions.outWidth
+ && candidate.getHeight() == targetOptions.outHeight
+ && targetOptions.inSampleSize == 1;
+}
+
+/**
+ * A helper function to return the byte usage per pixel of a bitmap based on its configuration.
+ */
+static int getBytesPerPixel(Config config) {
+ if (config == Config.ARGB_8888) {
+ return 4;
+ } else if (config == Config.RGB_565) {
+ return 2;
+ } else if (config == Config.ARGB_4444) {
+ return 2;
+ } else if (config == Config.ALPHA_8) {
+ return 1;
+ }
+ return 1;
}</pre>
</body>