From: David A. Velasco Date: Tue, 12 May 2015 11:32:01 +0000 (+0200) Subject: Minor changes after reviewing Bitmaps life cycle in PreviewImageFragment X-Git-Tag: test~6^2^2~5 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/10e4373bcfc7acdda25e91ea35e9ea8a8fb89c84?hp=-c Minor changes after reviewing Bitmaps life cycle in PreviewImageFragment --- 10e4373bcfc7acdda25e91ea35e9ea8a8fb89c84 diff --git a/src/com/owncloud/android/ui/preview/ImageViewCustom.java b/src/com/owncloud/android/ui/preview/ImageViewCustom.java index ad851404..f56557b6 100644 --- a/src/com/owncloud/android/ui/preview/ImageViewCustom.java +++ b/src/com/owncloud/android/ui/preview/ImageViewCustom.java @@ -12,10 +12,13 @@ import android.widget.ImageView; import com.owncloud.android.lib.common.utils.Log_OC; public class ImageViewCustom extends ImageView { - + + private static final String TAG = ImageViewCustom.class.getSimpleName(); + private static final boolean IS_ICS_OR_HIGHER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; - - private Bitmap mBitmap; + + private int mBitmapHeight; + private int mBitmapWidth; public ImageViewCustom(Context context) { @@ -45,26 +48,29 @@ public class ImageViewCustom extends ImageView { /** * Checks if current bitmaps exceed the maximum OpenGL texture size limit - * @param bitmap - * @return boolean + * @param canvas Canvas where the view will be drawn into. + * @return boolean True means that the bitmap is too big for the canvas. */ @SuppressLint("NewApi") private boolean checkIfMaximumBitmapExceed(Canvas canvas) { - Log_OC.d("OC", "Canvas maximum: " + canvas.getMaximumBitmapWidth() + " - " + canvas.getMaximumBitmapHeight()); - if (mBitmap!= null && (mBitmap.getWidth() > canvas.getMaximumBitmapWidth() - || mBitmap.getHeight() > canvas.getMaximumBitmapHeight())) { + Log_OC.v(TAG, "Canvas maximum: " + canvas.getMaximumBitmapWidth() + " - " + canvas.getMaximumBitmapHeight()); + if (mBitmapWidth > canvas.getMaximumBitmapWidth() + || mBitmapHeight > canvas.getMaximumBitmapHeight()) { return true; } return false; } + @Override /** - * Set current bitmap - * @param bitmap + * Keeps the size of the bitmap cached in member variables for faster access in {@link #onDraw(Canvas)} , + * but without keeping another reference to the {@link Bitmap} */ - public void setBitmap (Bitmap bitmap) { - mBitmap = bitmap; + public void setImageBitmap (Bitmap bm) { + mBitmapWidth = bm.getWidth(); + mBitmapHeight = bm.getHeight(); + super.setImageBitmap(bm); } } diff --git a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java index 9d1cd60f..18e799ba 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -202,12 +202,12 @@ public class PreviewImageFragment extends FileFragment { @Override public void onStop() { - super.onStop(); + Log_OC.d(TAG, "onStop starts"); if (mLoadBitmapTask != null) { mLoadBitmapTask.cancel(true); mLoadBitmapTask = null; } - + super.onStop(); } /** @@ -329,6 +329,8 @@ public class PreviewImageFragment extends FileFragment { if (mBitmap != null) { mBitmap.recycle(); System.gc(); + // putting this in onStop() is just the same; the fragment is always destroyed by the ViewPager + // when swipes further than the valid offset, and onStop() is never called before than that } super.onDestroy(); } @@ -415,7 +417,7 @@ public class PreviewImageFragment extends FileFragment { } } catch (OutOfMemoryError e) { - Log_OC.e(TAG, "Out of memory occured for file " + storagePath, e); + Log_OC.w(TAG, "Out of memory rendering file " + storagePath + " in full size; scaling down"); if (isCancelled()) return result; @@ -459,6 +461,10 @@ public class PreviewImageFragment extends FileFragment { } else { showErrorMessage(); } + if (mBitmap != result) { + // unused bitmap, release it! (just in case) + result.recycle(); + } } @SuppressLint("InlinedApi") @@ -466,11 +472,10 @@ public class PreviewImageFragment extends FileFragment { if (mImageViewRef != null) { final ImageViewCustom imageView = mImageViewRef.get(); if (imageView != null) { - imageView.setBitmap(result); imageView.setImageBitmap(result); imageView.setVisibility(View.VISIBLE); - mBitmap = result; - } // else , silently finish, the fragment was destroyed + mBitmap = result; // needs to be kept for recycling when not useful + } } if (mMessageViewRef != null) { final TextView messageView = mMessageViewRef.get();