Skip to content

Commit

Permalink
1.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
CPPAlien committed Sep 8, 2016
1 parent b4a256e commit 005be8a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ getHttpRequest().timeOut(int timesOutMs)
```
getHttpRequest().maxRetries(int maxRetries)
```
* 设置加载图片大小,图片长宽按比例缩放为设定的maxpix大小,并且缓存到本地
* 设置加载图片大小,下载后图片长宽按比例缩放为设定的maxpix大小,并且使用url + maxPix的方式作为key, 缓存到本地
```
getImageLoader().resize(int maxPix).load(...)
```
**注:对Gif无效;**
**注:1, 对Gif无效;2, 如果不想使用maxPix作为key的一部分,可以使用resize(int maxPix, 0);**

* 使用POST方法加载图片,body中为post方法体
```
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/cn/hadcn/davinci_example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cn.hadcn.davinci_example;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -14,6 +17,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.android.tools.build:gradle:2.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions davinci/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {

defaultConfig {
minSdkVersion 10
versionCode 2016062201
versionName "1.2.1"
versionCode 2016090801
versionName "1.3.5"
}
buildTypes {
release {
Expand Down
19 changes: 8 additions & 11 deletions davinci/src/main/java/cn/hadcn/davinci/image/ReadImageTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ReadImageTask {
private ImageLoader mImageLoader;
private Context mContext;
private int mMaxSize;
private int mKeyMode;

public ReadImageTask(Context context, VinciImageLoader.ImageCache imageCache, ImageLoader imageLoader, String imageUrl) {
mImageUrl = imageUrl;
Expand All @@ -40,11 +41,12 @@ public final void execute(String requestBody) {
mImageView.setImageDrawable(mContext.getResources().getDrawable(mErrorImage));
return;
}

ImageEntity entity = mImageCache.getBitmap(Util.generateKey(mImageUrl + mMaxSize));
String rawKey = mImageUrl;
if ( mKeyMode != 0 && mMaxSize != 0 ) rawKey += mMaxSize;
ImageEntity entity = mImageCache.getBitmap(Util.generateKey(rawKey));

if ( entity != null ) {
VinciLog.d("Load image from cache, key = " + Util.generateKey(mImageUrl + mMaxSize));
VinciLog.d("Load image from cache, key = " + Util.generateKey(rawKey));

// if it's gif, show as gif
if ( entity.isGif() ) {
Expand All @@ -60,7 +62,7 @@ public final void execute(String requestBody) {
} else if ( mImageUrl.startsWith("http") ) {
VolleyImageListener listener = new VolleyImageListener(mContext, mImageView, mImageCache);
listener.setDefaultImage(mLoadingImage, mErrorImage);
listener.setMaxSize(mMaxSize);
listener.setMaxSize(mMaxSize, mKeyMode);
VinciLog.d("Load image from web, url = " + mImageUrl );
mImageLoader.get(mImageUrl, requestBody, listener);
} else {
Expand All @@ -78,13 +80,8 @@ protected void setView(ImageView imageView) {
mImageView = imageView;
}

protected void setSize(int size) {
protected void setSize(int size, int mode) {
mMaxSize = size;
}

private void throwIfNotOnMainThread() {
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("ImageLoader must be invoked from the main thread.");
}
mKeyMode = mode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class VinciImageLoader {
private ImageLoader mImageLoader;
private Context mContext;
private int mMaxSize = 0;
private int mKeyMode = 0;
private final static int CACHE_SIZE = 1024 * 1024 * 50;
private ReadImageTask mReadImageTask;

Expand Down Expand Up @@ -106,7 +107,7 @@ public void into(ImageView imageView) {

public void into(ImageView imageView, int loadingImage, int errorImage) {
mReadImageTask.setView(imageView, loadingImage, errorImage);
mReadImageTask.setSize(mMaxSize);
mReadImageTask.setSize(mMaxSize, mKeyMode);
if ( mBody != null ) mReadImageTask.execute(mBody);
else mReadImageTask.execute(gBody);
mBody = null;
Expand All @@ -119,6 +120,19 @@ public void into(ImageView imageView, int loadingImage, int errorImage) {
*/
public VinciImageLoader resize(int maxPix) {
mMaxSize = maxPix;
mKeyMode = 1;
return this;
}

/**
* limit the max size of an image will be displayed, height and width are both shorter than maxPix
* @param maxPix max pixels of height and width
* @param mode save mode, mode = 0, do not use maxPix as image save key
* @return DaImageLoader instance
*/
public VinciImageLoader resize(int maxPix, int mode) {
mMaxSize = maxPix;
mKeyMode = mode;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class VolleyImageListener implements ImageLoader.ImageListener {
private int mLoadingImage;
private int mErrorImage;
private int mMaxSize = 0;
private int mKeyMode = 0;
private Context mContext;
private VinciImageLoader.ImageCache mImageCache;

Expand All @@ -32,8 +33,9 @@ protected VolleyImageListener(Context context, ImageView imageView, VinciImageLo
mImageCache = imageCache;
}

protected void setMaxSize(int size) {
protected void setMaxSize(int size, int mode) {
mMaxSize = size;
mKeyMode = mode;
}

protected void setDefaultImage(int loadingImage, int errorImage) {
Expand Down Expand Up @@ -87,7 +89,10 @@ public void onErrorResponse(VolleyError error) {
}

private void cacheImage(String url, byte[] data) {
String key = Util.generateKey(url + mMaxSize);
String rawKey = url;
if ( mKeyMode != 0 && mMaxSize != 0 ) rawKey += mMaxSize;

String key = Util.generateKey(rawKey);
if ( key.isEmpty() ) return;
mImageCache.putBitmap(key, data);
}
Expand Down

0 comments on commit 005be8a

Please sign in to comment.