Skip to content

Commit

Permalink
图片文件缓存优化,提升效率
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqp2010 committed Dec 24, 2013
1 parent 41f85dc commit df197bb
Show file tree
Hide file tree
Showing 147 changed files with 1,271 additions and 291 deletions.
Binary file added AndBase.zip
Binary file not shown.
Binary file modified AndBase/bin/andbase.jar
Binary file not shown.
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageCache$1.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageCache.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloadPool$1.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloadPool$2.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloadPool.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloadQueue.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloadTask$1.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloadTask.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloader$1.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/bitmap/AbImageDownloader.class
Binary file not shown.
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/download/AbDownloadThread.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/download/AbFileDownloader.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/download/DownFile.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/download/DownFileDao.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/global/AbAppData.class
Binary file not shown.
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/util/AbFileUtil.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/util/AbImageUtil.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/titlebar/AbBottomBar$1.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/titlebar/AbBottomBar.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/titlebar/AbTitleBar$1.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/titlebar/AbTitleBar$2.class
Binary file not shown.
Binary file modified AndBase/bin/classes/com/ab/view/titlebar/AbTitleBar.class
Binary file not shown.
146 changes: 146 additions & 0 deletions AndBase/src/com/ab/bitmap/AbFileCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (C) 2013 www.418log.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ab.bitmap;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

import com.ab.global.AbAppData;
import com.ab.util.AbFileUtil;
import com.ab.util.AbStrUtil;

// TODO: Auto-generated Javadoc
/**
* 描述:图片SD卡缓存管理.
*
* @author zhaoqp
* @date:2013-5-23 上午10:10:53
* @version v1.0
*/

public class AbFileCache {

/** The tag. */
private static String TAG = "AbFileCache";

/** The Constant D. */
private static final boolean D = AbAppData.DEBUG;

/** 10MB. */
public static int maxCacheSize = 10 * 1024 * 1024;

/** 当前缓存大小. */
public static int cacheSize = 0;

/** 文件缓存(文件名,文件) */
private static final HashMap<String, File> fileCache = new HashMap<String, File>();

/**锁对象*/
public static final ReentrantLock lock = new ReentrantLock();

static {
//初始化缓存
AbFileUtil.initFileCache();
}

/**
* 描述:从缓存中获取这个File.
*
* @param name 文件名
* @return the file from mem cache
*/
public static File getFileFromCache(String name) {
return fileCache.get(name);
}

/**
* 描述:增加一个File到缓存.
*
* @param name 文件名
* @param bitmap the bitmap
*/
public static void addFileToCache(String name,File file){
try {
lock.lock();
if(AbStrUtil.isEmpty(name)){
return;
}

if (getFileFromCache(name) == null && file!=null) {
fileCache.put(name, file);
}

//当前大小大于预定缓存空间
if (cacheSize > maxCacheSize) {
//释放部分文件
AbFileUtil.freeCacheFiles();
}
} catch (Exception e) {
e.printStackTrace();
} finally{
lock.unlock();
}
}

/**
* 描述:从缓存删除.
*
* @param name 文件名
*/
public static void removeFileFromCache(String name){
try {
lock.lock();
if (getFileFromCache(name) != null) {
fileCache.remove(name);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
lock.unlock();
}
}

/**
* 描述:清空缓存的Bitmap.
*/
public static void removeAllFileFromCache() {
AbFileUtil.removeAllFileCache();
fileCache.clear();
}


/**
* 设置缓存空间大小
* @param cacheSize
*/
public static void setMaxCacheSize(int cacheSize) {
AbFileCache.maxCacheSize = cacheSize;
}

/**
*
* 描述:缓存文件夹的大小
* @return
* @throws
*/
public static int getCacheSize() {
return cacheSize;
}



}
19 changes: 15 additions & 4 deletions AndBase/src/com/ab/bitmap/AbImageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ public class AbImageCache {
/** The Constant D. */
private static final boolean D = AbAppData.DEBUG;

/** 4MB. */
public static int cacheSize = 10 * 1024 * 1024;
/** 缓存空间大小8MB. */
public static int cacheSize = 8 * 1024 * 1024;

/** 为了加快速度,在内存中开启缓存,最新的LruCache. */
private static final LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}};
}

@Override
protected void entryRemoved(boolean evicted, String key,
Bitmap oldValue, Bitmap newValue) {
if(D) Log.d(TAG, "LruCache:移除了"+key);
}


};

/**正在下载中的线程*/
private static final HashMap<String, Runnable> runRunnableCache = new HashMap<String, Runnable>();
Expand Down Expand Up @@ -90,6 +99,7 @@ public static void addBitmapToCache(String key,Bitmap bitmap){
if (getBitmapFromCache(key) == null && bitmap!=null) {
bitmapCache.put(key, bitmap);
if(D) Log.d(TAG, "存入缓存:"+key+","+bitmap);
if(D) Log.d(TAG, "测试存入缓存是否成功:"+key+","+getBitmapFromCache(key));
}
//表示下载中的缓存清除
removeRunRunnableFromCache(key);
Expand Down Expand Up @@ -129,7 +139,7 @@ public static void removeAllBitmapFromCache() {
}

/**
* 根据url计算缓存key.
* 根据url计算缓存key,这个key+后缀就是文件名.
* @param url 图片地址.
* @param width 图片宽度.
* @param height 图片高度.
Expand Down Expand Up @@ -241,5 +251,6 @@ public static void removeWaitRunnableFromCache(String key){
lock.unlock();
}
}


}
2 changes: 1 addition & 1 deletion AndBase/src/com/ab/bitmap/AbImageDownloadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import android.os.Handler;
import android.os.Message;
Expand Down Expand Up @@ -146,6 +145,7 @@ public void run() {
});

}else{
if(D) Log.d(TAG, "从内存缓存中得到图片:"+cacheKey+","+item.bitmap);
if (item.listener != null) {
Message msg = handler.obtainMessage();
msg.obj = item;
Expand Down
1 change: 1 addition & 0 deletions AndBase/src/com/ab/bitmap/AbImageDownloadQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void download(AbImageDownloadItem item) {
if(item.bitmap == null){
addDownloadItem(item);
}else{
if(D) Log.d(TAG, "从内存缓存中得到图片:"+cacheKey+","+item.bitmap);
if (item.listener != null) {
Message msg = handler.obtainMessage();
msg.obj = item;
Expand Down
9 changes: 5 additions & 4 deletions AndBase/src/com/ab/bitmap/AbImageDownloadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import com.ab.global.AbAppData;
import com.ab.util.AbFileUtil;
import com.ab.util.AbMd5;
import com.ab.util.AbStrUtil;
/**
*
Expand Down Expand Up @@ -56,20 +55,22 @@ protected AbImageDownloadItem doInBackground(AbImageDownloadItem... items) {
}else{
url = url.trim();
}
//如果SD卡有这个图片, 先到SD中找这个图片
item.bitmap = AbImageCache.getBitmapFromCache(AbMd5.MD5(url+"_"+item.width+"x"+item.height+"t"+item.type));

String cacheKey = AbImageCache.getCacheKey(url, item.width, item.height, item.type);
item.bitmap = AbImageCache.getBitmapFromCache(cacheKey);
if(item.bitmap == null){
//开始下载
item.bitmap = AbFileUtil.getBitmapFromSDCache(item.imageUrl,item.type,item.width,item.height);
//缓存图片路径
AbImageCache.addBitmapToCache(AbMd5.MD5(item.imageUrl+"_"+item.width+"x"+item.height+"t"+item.type),item.bitmap);
AbImageCache.addBitmapToCache(cacheKey,item.bitmap);
//需要执行回调来显示图片
if (item.listener != null) {
Message msg = handler.obtainMessage();
msg.obj = item;
handler.sendMessage(msg);
}
}else{
if(D) Log.d(TAG, "从内存缓存中得到图片:"+cacheKey+","+item.bitmap);
if (item.listener != null) {
Message msg = handler.obtainMessage();
msg.obj = item;
Expand Down
2 changes: 0 additions & 2 deletions AndBase/src/com/ab/bitmap/AbImageDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.ab.global.AbAppData;
import com.ab.global.AbConstant;
import com.ab.util.AbImageUtil;
import com.ab.util.AbMd5;
import com.ab.util.AbStrUtil;

// TODO: Auto-generated Javadoc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface AbDownloadProgressListener {
*
* @param size the size
*/
public void onDownloadSize(int size);
public void onDownloadSize(long size);
}


12 changes: 6 additions & 6 deletions AndBase/src/com/ab/download/DownFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public class DownFile {
private String downPath;

/** 文件当前下载大小. */
private int downLength;
private long downLength;

/** 文件总大小. */
private int totalLength;
private long totalLength;

/** 文件类型后缀.tmp. */
private String suffix;
Expand Down Expand Up @@ -208,7 +208,7 @@ public void setDownPath(String downPath) {
*
* @return the down length
*/
public int getDownLength() {
public long getDownLength() {
return downLength;
}

Expand All @@ -217,7 +217,7 @@ public int getDownLength() {
*
* @param downLength the new down length
*/
public void setDownLength(int downLength) {
public void setDownLength(long downLength) {
this.downLength = downLength;
}

Expand All @@ -226,7 +226,7 @@ public void setDownLength(int downLength) {
*
* @return the total length
*/
public int getTotalLength() {
public long getTotalLength() {
return totalLength;
}

Expand All @@ -235,7 +235,7 @@ public int getTotalLength() {
*
* @param totalLength the new total length
*/
public void setTotalLength(int totalLength) {
public void setTotalLength(long totalLength) {
this.totalLength = totalLength;
}

Expand Down
2 changes: 1 addition & 1 deletion AndBase/src/com/ab/global/AbAppData.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class AbAppData {

/** 日志开关. */
public static boolean DEBUG = true;
public static boolean DEBUG = false;

/** 性能测试. */
public static boolean mMonitorOpened = false;
Expand Down
Loading

0 comments on commit df197bb

Please sign in to comment.