diff options
author | John Reck <jreck@google.com> | 2013-08-08 14:16:24 -0700 |
---|---|---|
committer | John Reck <jreck@google.com> | 2013-08-08 14:19:07 -0700 |
commit | c31aee6a67b08716b916906b2db2096e1b929b4a (patch) | |
tree | fbcc53d78d17d4cc6ab4dd608ac45fa1a26113c8 /src/com/android/photos | |
parent | a6b80cccafe0a4c2de575ec69d1960c3c0d88062 (diff) | |
download | android_packages_apps_Gallery2-c31aee6a67b08716b916906b2db2096e1b929b4a.tar.gz android_packages_apps_Gallery2-c31aee6a67b08716b916906b2db2096e1b929b4a.tar.bz2 android_packages_apps_Gallery2-c31aee6a67b08716b916906b2db2096e1b929b4a.zip |
Sync latest TiledImageView
Change-Id: I57e040a05e0ae4c1886eda014f1e6bf750612a3e
Diffstat (limited to 'src/com/android/photos')
-rw-r--r-- | src/com/android/photos/BitmapRegionTileSource.java | 51 | ||||
-rw-r--r-- | src/com/android/photos/views/TiledImageView.java | 4 |
2 files changed, 47 insertions, 8 deletions
diff --git a/src/com/android/photos/BitmapRegionTileSource.java b/src/com/android/photos/BitmapRegionTileSource.java index d7d52f67a..d2d0570d2 100644 --- a/src/com/android/photos/BitmapRegionTileSource.java +++ b/src/com/android/photos/BitmapRegionTileSource.java @@ -16,6 +16,7 @@ package com.android.photos; +import android.annotation.TargetApi; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; @@ -27,6 +28,7 @@ import android.os.Build; import android.os.Build.VERSION_CODES; import android.util.Log; +import com.android.gallery3d.common.BitmapUtils; import com.android.gallery3d.glrenderer.BasicTexture; import com.android.gallery3d.glrenderer.BitmapTexture; import com.android.photos.views.TiledImageRenderer; @@ -37,12 +39,16 @@ import java.io.IOException; * A {@link com.android.photos.views.TiledImageRenderer.TileSource} using * {@link BitmapRegionDecoder} to wrap a local file */ +@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class BitmapRegionTileSource implements TiledImageRenderer.TileSource { private static final String TAG = "BitmapRegionTileSource"; private static final boolean REUSE_BITMAP = Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; + private static final int GL_SIZE_LIMIT = 2048; + // This must be no larger than half the size of the GL_SIZE_LIMIT + // due to decodePreview being allowed to be up to 2x the size of the target private static final int MAX_PREVIEW_SIZE = 1024; BitmapRegionDecoder mDecoder; @@ -77,18 +83,14 @@ public class BitmapRegionTileSource implements TiledImageRenderer.TileSource { // Although this is the same size as the Bitmap that is likely already // loaded, the lifecycle is different and interactions are on a different // thread. Thus to simplify, this source will decode its own bitmap. - int sampleSize = (int) Math.ceil(Math.max( - mWidth / (float) previewSize, mHeight / (float) previewSize)); - mOptions.inSampleSize = Math.max(sampleSize, 1); - Bitmap preview = mDecoder.decodeRegion( - new Rect(0, 0, mWidth, mHeight), mOptions); - if (preview.getWidth() <= MAX_PREVIEW_SIZE && preview.getHeight() <= MAX_PREVIEW_SIZE) { + Bitmap preview = decodePreview(path, previewSize); + if (preview.getWidth() <= GL_SIZE_LIMIT && preview.getHeight() <= GL_SIZE_LIMIT) { mPreview = new BitmapTexture(preview); } else { Log.w(TAG, String.format( "Failed to create preview of apropriate size! " - + " in: %dx%d, sample: %d, out: %dx%d", - mWidth, mHeight, sampleSize, + + " in: %dx%d, out: %dx%d", + mWidth, mHeight, preview.getWidth(), preview.getHeight())); } } @@ -180,4 +182,37 @@ public class BitmapRegionTileSource implements TiledImageRenderer.TileSource { mCanvas.setBitmap(null); return result; } + + /** + * Note that the returned bitmap may have a long edge that's longer + * than the targetSize, but it will always be less than 2x the targetSize + */ + private Bitmap decodePreview(String file, int targetSize) { + float scale = (float) targetSize / Math.max(mWidth, mHeight); + mOptions.inSampleSize = BitmapUtils.computeSampleSizeLarger(scale); + mOptions.inJustDecodeBounds = false; + + Bitmap result = BitmapFactory.decodeFile(file, mOptions); + if (result == null) { + return null; + } + + // We need to resize down if the decoder does not support inSampleSize + // or didn't support the specified inSampleSize (some decoders only do powers of 2) + scale = (float) targetSize / (float) (Math.max(result.getWidth(), result.getHeight())); + + if (scale <= 0.5) { + result = BitmapUtils.resizeBitmapByScale(result, scale, true); + } + return ensureGLCompatibleBitmap(result); + } + + private static Bitmap ensureGLCompatibleBitmap(Bitmap bitmap) { + if (bitmap == null || bitmap.getConfig() != null) { + return bitmap; + } + Bitmap newBitmap = bitmap.copy(Config.ARGB_8888, false); + bitmap.recycle(); + return newBitmap; + } } diff --git a/src/com/android/photos/views/TiledImageView.java b/src/com/android/photos/views/TiledImageView.java index 8bc07c051..8d1cb486b 100644 --- a/src/com/android/photos/views/TiledImageView.java +++ b/src/com/android/photos/views/TiledImageView.java @@ -79,6 +79,10 @@ public class TiledImageView extends FrameLayout { private Object mLock = new Object(); private ImageRendererWrapper mRenderer; + public static boolean isTilingSupported() { + return IS_SUPPORTED; + } + public TiledImageView(Context context) { this(context, null); } |