Skip to content

Commit

Permalink
1. v1.1.0, breaking changes;
Browse files Browse the repository at this point in the history
2. prepare for multiple image load libraries support;
3. update perf data;
  • Loading branch information
Piasy committed Nov 8, 2016
1 parent d256093 commit 6fddbb7
Show file tree
Hide file tree
Showing 25 changed files with 278 additions and 146 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ android {
dependencies {
compile "com.android.support:support-annotations:$rootProject.ext.androidSupportSdkVersion"

compile "com.facebook.fresco:fresco:$rootProject.ext.frescoVersion"
compile "com.davemorrissey.labs:subsampling-scale-image-view:$rootProject.ext.ssivVersion"
compile "commons-io:commons-io:$rootProject.ext.commonsVersion"
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.piasy.fresco.bigimageviewer">
package="com.github.piasy.biv">

<uses-permission android:name="android.permission.INTERNET"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.piasy.biv;

import android.net.Uri;
import com.github.piasy.biv.loader.ImageLoader;

/**
* Created by Piasy{github.com/Piasy} on 06/11/2016.
*
* This is not a singleton, you can initialize it multiple times, but before you initialize it
* again, it will use the same {@link ImageLoader} globally.
*/

public final class BigImageViewer {
private static volatile BigImageViewer sInstance;

private final ImageLoader mImageLoader;

private BigImageViewer(ImageLoader imageLoader) {
mImageLoader = imageLoader;
}

public static void initialize(ImageLoader imageLoader) {
sInstance = new BigImageViewer(imageLoader);
}

public static ImageLoader imageLoader() {
if (sInstance == null) {
throw new IllegalStateException("You must initialize BigImageViewer before use it!");
}
return sInstance.mImageLoader;
}

public static void prefetch(Uri... uris) {
if (uris == null) {
return;
}

ImageLoader imageLoader = imageLoader();
for (Uri uri : uris) {
imageLoader.prefetch(uri);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.piasy.biv.loader;

import android.net.Uri;
import android.support.annotation.UiThread;
import android.support.annotation.WorkerThread;
import java.io.File;

/**
* Created by Piasy{github.com/Piasy} on 08/11/2016.
*/

public interface ImageLoader {

void loadImage(Uri uri, Callback callback);

void prefetch(Uri uri);

interface Callback {
@UiThread
void onCacheHit(File image);

@WorkerThread
void onCacheMiss(File image);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

package com.github.piasy.fresco.bigimageviewer;
package com.github.piasy.biv.view;

import android.content.Context;
import android.net.Uri;
Expand All @@ -34,32 +34,23 @@
import android.widget.FrameLayout;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.facebook.binaryresource.FileBinaryResource;
import com.facebook.cache.common.CacheKey;
import com.facebook.cache.disk.FileCache;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.cache.DefaultCacheKeyFactory;
import com.facebook.imagepipeline.core.DefaultExecutorSupplier;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import com.facebook.imagepipeline.request.ImageRequest;
import com.github.piasy.biv.BigImageViewer;
import com.github.piasy.biv.loader.ImageLoader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Piasy{github.com/Piasy} on 06/11/2016.
*
* Use FrameLayout for extensibility.
*/

public class BigImageView extends FrameLayout {
public class BigImageView extends FrameLayout implements ImageLoader.Callback {
private final SubsamplingScaleImageView mImageView;

private final DefaultExecutorSupplier mExecutorSupplier;
private final ImageLoader mImageLoader;

private final List<DataSource<CloseableReference<PooledByteBuffer>>> mDownloadingImages;
private final List<File> mTempImages;

public BigImageView(Context context) {
Expand All @@ -80,70 +71,41 @@ public BigImageView(Context context, AttributeSet attrs, int defStyleAttr) {
mImageView.setLayoutParams(params);
mImageView.setMinimumTileDpi(160);

mExecutorSupplier = new DefaultExecutorSupplier(Runtime.getRuntime().availableProcessors());
mDownloadingImages = new ArrayList<>();
mImageLoader = BigImageViewer.imageLoader();

mTempImages = new ArrayList<>();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();

for (int i = 0, size = mDownloadingImages.size(); i < size; i++) {
mDownloadingImages.get(i).close();
}
mDownloadingImages.clear();
for (int i = 0, size = mTempImages.size(); i < size; i++) {
mTempImages.get(i).delete();
}
mTempImages.clear();
}

public void showImage(Uri uri) {
Log.d("FrescoBigImageViewer", "showImage " + uri);

ImageRequest request = ImageRequest.fromUri(uri);

File localCache = getCacheFile(request);
if (localCache.exists()) {
showCachedImage(localCache);
} else {
ImagePipeline pipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<PooledByteBuffer>> source =
pipeline.fetchEncodedImage(request, true);
mDownloadingImages.add(source);
source.subscribe(new ImageDownloadSubscriber(getContext()) {
@Override
protected void onSuccess(File image) {
mTempImages.add(image);
showDownloadedImage(image);
}

@Override
protected void onFail(Throwable t) {
t.printStackTrace();
}
}, mExecutorSupplier.forBackgroundTasks());
}
Log.d("BigImageView", "showImage " + uri);

mImageLoader.loadImage(uri, this);
}

private File getCacheFile(final ImageRequest request) {
FileCache mainFileCache = ImagePipelineFactory
.getInstance()
.getMainFileCache();
final CacheKey cacheKey = DefaultCacheKeyFactory
.getInstance()
.getEncodedCacheKey(request, false); // we don't need context, but avoid null
File cacheFile = request.getSourceFile();
if (mainFileCache.hasKey(cacheKey)) {
cacheFile = ((FileBinaryResource) mainFileCache.getResource(cacheKey)).getFile();
}
return cacheFile;
@UiThread
@Override
public void onCacheHit(File image) {
Log.d("BigImageView", "onCacheHit " + image);

doShowImage(image);
}

@WorkerThread
private void showDownloadedImage(final File image) {
Log.d("FrescoBigImageViewer", "showDownloadedImage " + image);
@Override
public void onCacheMiss(final File image) {
Log.d("BigImageView", "onCacheMiss " + image);

mTempImages.add(image);
post(new Runnable() {
@Override
public void run() {
Expand All @@ -152,12 +114,6 @@ public void run() {
});
}

@UiThread
private void showCachedImage(File image) {
Log.d("FrescoBigImageViewer", "showCachedImage " + image);
doShowImage(image);
}

@UiThread
private void doShowImage(File image) {
mImageView.setImage(ImageSource.uri(Uri.fromFile(image)));
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions FrescoImageLoader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions FrescoImageLoader/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apply plugin: 'com.android.library'
apply from: "https://raw.githubusercontent.com/Piasy/BintrayUploadScript/master/bintray.gradle"

android {
compileSdkVersion rootProject.ext.androidCompileSdkVersion
buildToolsVersion rootProject.ext.androidBuildToolsVersion

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.releaseVersionCode
versionName rootProject.ext.releaseVersionName
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile project(':BigImageViewer')

compile "com.facebook.fresco:fresco:$rootProject.ext.frescoVersion"
compile "commons-io:commons-io:$rootProject.ext.commonsVersion"
}
17 changes: 17 additions & 0 deletions FrescoImageLoader/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/piasy/tools/android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
1 change: 1 addition & 0 deletions FrescoImageLoader/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.github.piasy.biv.loader.fresco"/>
Loading

0 comments on commit 6fddbb7

Please sign in to comment.