diff --git a/twidere/build.gradle b/twidere/build.gradle index 8f18150e3..a52173325 100644 --- a/twidere/build.gradle +++ b/twidere/build.gradle @@ -62,6 +62,7 @@ android { repositories { maven { url 'https://s3.amazonaws.com/repo.commonsware.com' } maven { url 'https://maven.fabric.io/public' } + maven { url 'https://github.com/suckgamony/RapidDecoder/raw/master/repository' } flatDir { dirs "$projectDir/lib" } } @@ -108,10 +109,14 @@ dependencies { compile 'com.lnikkila:extendedtouchview:0.1.0' compile 'com.google.dagger:dagger:2.0.2' compile 'org.attoparser:attoparser:1.4.0.RELEASE' - compile 'com.github.mariotaku.MediaViewerLibrary:base:0.9.10' - compile 'com.github.mariotaku.MediaViewerLibrary:subsample-image-view:0.9.10' + compile 'com.github.mariotaku.MediaViewerLibrary:base:0.9.11' + compile 'com.github.mariotaku.MediaViewerLibrary:subsample-image-view:0.9.11' compile 'com.github.mariotaku.SQLiteQB:library:0.9.5' compile 'com.github.mariotaku.ObjectCursor:core:0.9.5' + compile 'rapid.decoder:library:0.3.0' + compile 'rapid.decoder:jpeg-decoder:0.3.0' + compile 'rapid.decoder:png-decoder:0.3.0' + compile project(':twidere.component.common') compile project(':twidere.component.nyan') diff --git a/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java b/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java index 725fc0b8d..6a2d4bc39 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java +++ b/twidere/src/main/java/org/mariotaku/twidere/activity/support/MediaViewerActivity.java @@ -65,6 +65,7 @@ import android.widget.TextView; import android.widget.Toast; import com.commonsware.cwac.layouts.AspectLockedFrameLayout; +import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.pnikosis.materialishprogress.ProgressWheel; import com.sprylab.android.widget.TextureVideoView; @@ -92,6 +93,8 @@ import org.mariotaku.twidere.util.PermissionUtils; import org.mariotaku.twidere.util.ThemeUtils; import org.mariotaku.twidere.util.Utils; import org.mariotaku.twidere.util.dagger.GeneralComponentHelper; +import org.mariotaku.twidere.util.imageviewer.RapidImageDecoder; +import org.mariotaku.twidere.util.imageviewer.RapidImageRegionDecoder; import org.mariotaku.twidere.util.media.MediaExtra; import java.io.File; @@ -854,6 +857,12 @@ public final class MediaViewerActivity extends AbsMediaViewerActivity implements activity.supportInvalidateOptionsMenu(); } } + + @Override + protected void setupImageView(SubsamplingScaleImageView imageView) { + imageView.setRegionDecoderClass(RapidImageRegionDecoder.class); + imageView.setBitmapDecoderClass(RapidImageDecoder.class); + } } public static class VideoPageFragment extends CacheDownloadMediaViewerFragment diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/imageviewer/RapidImageDecoder.java b/twidere/src/main/java/org/mariotaku/twidere/util/imageviewer/RapidImageDecoder.java new file mode 100644 index 000000000..88a870af8 --- /dev/null +++ b/twidere/src/main/java/org/mariotaku/twidere/util/imageviewer/RapidImageDecoder.java @@ -0,0 +1,26 @@ +package org.mariotaku.twidere.util.imageviewer; + +import android.content.Context; +import android.graphics.Bitmap; +import android.net.Uri; + +import com.davemorrissey.labs.subscaleview.decoder.ImageDecoder; + +import rapid.decoder.BitmapDecoder; + +/** + * A very simple implementation of {@link com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder} + * using the RapidDecoder library (https://github.com/suckgamony/RapidDecoder). For PNGs, this can + * give more reliable decoding and better performance. For JPGs, it is slower and can run out of + * memory with large images, but has better support for grayscale and CMYK images. + * + * This is an incomplete and untested implementation provided as an example only. + */ +public class RapidImageDecoder implements ImageDecoder { + + @Override + public Bitmap decode(Context context, Uri uri) throws Exception { + return BitmapDecoder.from(context, uri).useBuiltInDecoder(true).config(Bitmap.Config.RGB_565).decode(); + } + +} \ No newline at end of file diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/imageviewer/RapidImageRegionDecoder.java b/twidere/src/main/java/org/mariotaku/twidere/util/imageviewer/RapidImageRegionDecoder.java new file mode 100644 index 000000000..aa3d93687 --- /dev/null +++ b/twidere/src/main/java/org/mariotaku/twidere/util/imageviewer/RapidImageRegionDecoder.java @@ -0,0 +1,57 @@ +package org.mariotaku.twidere.util.imageviewer; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Point; +import android.graphics.Rect; +import android.net.Uri; + +import com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder; + +import rapid.decoder.BitmapDecoder; + +/** + * A very simple implementation of {@link com.davemorrissey.labs.subscaleview.decoder.ImageRegionDecoder} + * using the RapidDecoder library (https://github.com/suckgamony/RapidDecoder). For PNGs, this can + * give more reliable decoding and better performance. For JPGs, it is slower and can run out of + * memory with large images, but has better support for grayscale and CMYK images. + *

+ * This is an incomplete and untested implementation provided as an example only. + */ +public class RapidImageRegionDecoder implements ImageRegionDecoder { + + private BitmapDecoder decoder; + + @Override + public Point init(Context context, Uri uri) throws Exception { + decoder = BitmapDecoder.from(context, uri); + decoder.useBuiltInDecoder(true); + return new Point(decoder.sourceWidth(), decoder.sourceHeight()); + } + + @Override + public synchronized Bitmap decodeRegion(Rect sRect, int sampleSize) { + try { + return decoder.reset().region(sRect).scale(sRect.width() / sampleSize, sRect.height() / sampleSize).decode(); + } catch (Exception e) { + return null; + } + } + + @Override + public boolean isReady() { + return decoder != null; + } + + @Override + public void recycle() { + BitmapDecoder.destroyMemoryCache(); + BitmapDecoder.destroyDiskCache(); + try { + decoder.reset(); + } catch (UnsupportedOperationException ignore) { + + } + decoder = null; + } +} \ No newline at end of file