Skip to content

Commit

Permalink
优化Bitmap配置
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Aug 23, 2014
1 parent e84ee97 commit 84e103a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
9 changes: 2 additions & 7 deletions library/src/com/lidroid/xutils/BitmapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public BitmapUtils(Context context, String diskCachePath) {
throw new IllegalArgumentException("context may not be null");
}

this.context = context;
globalConfig = new BitmapGlobalConfig(context, diskCachePath);
this.context = context.getApplicationContext();
globalConfig = BitmapGlobalConfig.getInstance(this.context, diskCachePath);
defaultDisplayConfig = new BitmapDisplayConfig();
}

Expand Down Expand Up @@ -199,11 +199,6 @@ public BitmapUtils configBitmapCacheListener(BitmapCacheListener listener) {
return this;
}

public BitmapUtils configGlobalConfig(BitmapGlobalConfig globalConfig) {
this.globalConfig = globalConfig;
return this;
}

////////////////////////// display ////////////////////////////////////

public <T extends View> void display(T container, String uri) {
Expand Down
29 changes: 22 additions & 7 deletions library/src/com/lidroid/xutils/bitmap/BitmapGlobalConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.lidroid.xutils.bitmap.core.BitmapCache;
import com.lidroid.xutils.bitmap.download.DefaultDownloader;
import com.lidroid.xutils.bitmap.download.Downloader;
import com.lidroid.xutils.cache.FileNameGenerator;
import com.lidroid.xutils.task.Priority;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.util.OtherUtils;
import com.lidroid.xutils.task.PriorityAsyncTask;
import com.lidroid.xutils.cache.FileNameGenerator;
import com.lidroid.xutils.task.PriorityExecutor;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.util.OtherUtils;

import java.util.HashMap;

/**
* Author: wyouflf
Expand Down Expand Up @@ -60,27 +62,40 @@ public class BitmapGlobalConfig {
private BitmapCacheListener bitmapCacheListener;

private Context mContext;
private final static HashMap<String, BitmapGlobalConfig> configMap = new HashMap<String, BitmapGlobalConfig>(1);

/**
* @param context
* @param diskCachePath If null, use default appCacheDir+"/xBitmapCache"
*/
public BitmapGlobalConfig(Context context, String diskCachePath) {
private BitmapGlobalConfig(Context context, String diskCachePath) {
if (context == null) throw new IllegalArgumentException("context may not be null");
this.mContext = context;
this.diskCachePath = diskCachePath;
initBitmapCache();
}

public static BitmapGlobalConfig getInstance(Context context, String diskCachePath) {

if (TextUtils.isEmpty(diskCachePath)) {
diskCachePath = OtherUtils.getDiskCacheDir(context, "xBitmapCache");
}

if (configMap.containsKey(diskCachePath)) {
return configMap.get(diskCachePath);
} else {
BitmapGlobalConfig config = new BitmapGlobalConfig(context, diskCachePath);
configMap.put(diskCachePath, config);
return config;
}
}

private void initBitmapCache() {
new BitmapCacheManagementTask().execute(BitmapCacheManagementTask.MESSAGE_INIT_MEMORY_CACHE);
new BitmapCacheManagementTask().execute(BitmapCacheManagementTask.MESSAGE_INIT_DISK_CACHE);
}

public String getDiskCachePath() {
if (TextUtils.isEmpty(diskCachePath)) {
diskCachePath = OtherUtils.getDiskCacheDir(mContext, "xBitmapCache");
}
return diskCachePath;
}

Expand Down

0 comments on commit 84e103a

Please sign in to comment.