Bitmap的高效加载
核心思想:用 BitmapFactory.Options 来加载合适尺寸的图片。通过BitmapFactory.Options 设置采样率来压缩图片。
拿一张1024 * 1024 像素的图片来说,假定采用ARGB8888格式存储,那么它占有的内存为 1024 * 1024 * 4 Bytes,即4MB,如果ImageView的尺寸为512 * 512,那么就没必要把全尺寸的图片加载到ImageView中。BitmapFactory.Options 中有一个field叫 inSampleSize,即采样率,默认值是1,如果设置该值小于1则无任何效果,如果设置为大于1的值,则缩放后图片占用内存为 1 / (inSampleSize ^ 2)。还用刚才的例子,如果把 1024 * 1024的图片设置到 512 * 512 的ImageView中,只需设置 inSampleSize = 2:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
return BitmapFactory.decodeFromResource(res, resId, options);
怎样计算加载图片占用内存的大小?
图片占用内存的大小取决于3个变量:
- 宽
- 高
- 存储格式
宽和高很好理解,就是图片在横向和纵向上的像素数。存储格式决定了每个像素占用多少内存。常见的存储格式为 ARGB8888,我们就以它为例。
ARGB8888的意思是 A(Alpha),R(Red),G(Green),B(Blue)四个通道,每个通道占用8bit,一共32bit,也就是4byte
此外还有 RGB565,RGB888等等格式,后面的数字就代表对应的通道所占的bit数,把所有数字加起来就是1个像素占有的bit数,也就可以算出byte值。
怎样计算采样率?
采样率的计算也需要依赖 BitmapFactory.Options。当 BitmapFactory.Options的 inJustDecodeBounds 参数设置为true时,BitmapFactory只会解析图片的原始宽/高信息,并不会去真正加载图片。有了图片的宽高和ImageView的宽高,我们就可以计算出合适的采样率:
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
// 把 inJustDecodeBounds设为true,BitmapFactory就不会把图片加载到内存,只会去计算图片的尺寸
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
if (reqWidth == 0 || reqHeight == 0) {
return 1;
}
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqHeight) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Android 中的缓存策略
LruCache
LruCache 是Android提供的缓存类,实现LRU算法,底层用LinkedHashMap来存储需要缓存的对象。这个类已经纳入到Android源码当中,一般被用来作为内存中的缓存使用。
DiskLruCache
顾名思义,这个类实现的是磁盘缓存。虽然它得到了Android官方文档的推荐,但目前并不属于Android SDK的一部分,所以我们发现像Glide和OkHttp这些开源框架里都用到了这个类,但都针对自己的情况作了修改。
ImageLoader的实现
如果要自己实现一个ImageLoader,那么一般要具备如下功能:
- 图片的同步加载
- 图片的异步加载
- 图片压缩 (计算采样率)
- 内存缓存
- 磁盘缓存
- 网络拉取
完整代码:
package com.anjiawei.httpdemo.bitmap.imageloader;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.StatFs;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.internal.cache.DiskLruCache;
public class ImageLoader {
private static final String TAG = "ImageLoader";
private static final int DISK_CACHE_SIZE = 1024 * 1024 * 50;
private static final int IO_BUFFER_SIZE = 1024;
private static final int MSG_POST_RESULT = 0;
private static final int TAG_KEY_URL = 0;
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
private static final int MAX_POOL_SIZE = CPU_COUNT * 2 + 1;
private static final long KEEP_ALIVE = 10L;
private static final ThreadFactory sThreadFactory = new ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "ImageLoader#" + mCount.getAndIncrement());
}
};
public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE,
MAX_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, new LinkedBlockingQueue(), sThreadFactory);
private LruCache mMemoryCache;
private DiskLruCache mDiskLruCache;
private Context mContext;
private boolean mIsDiskLruCacheCreated;
private ImageResizer mImageResizer;
private Handler mMainHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
LoaderResult result = (LoaderResult) msg.obj;
ImageView imageView = result.mImageView;
String url = (String) imageView.getTag(TAG_KEY_URL);
if (url.equals(result.mUrl)) {
imageView.setImageBitmap(result.mBitmap);
} else {
Log.w(TAG, "set image bitmap, but url has changed, ignored");
}
}
};
private ImageLoader(Context context) {
mContext = context.getApplicationContext();
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
File diskCacheDir = getDiskCacheDir(mContext, "bitmap");
if (!diskCacheDir.exists()) {
diskCacheDir.mkdirs();
}
if (getUsableSpace(diskCacheDir) > DISK_CACHE_SIZE) {
try {
mDiskLruCache = DiskLruCache.open(diskCacheDir, 1, 1, DISK_CACHE_SIZE);
mIsDiskLruCacheCreated = true;
} catch (IOException e) {
e.printStackTrace();
}
}
mImageResizer = new ImageResizer();
}
public static ImageLoader getInstance(Context context) {
return new ImageLoader(context);
}
/**
* load bitmap from memory cache or disk or network
* This is a synchronized method
*
* @param url
* @param reqWidth
* @param reqHeight
* @return
*/
public Bitmap loadBitmap(String url, int reqWidth, int reqHeight) {
Bitmap bitmap = loadBitmapFromMemCache(url);
if (bitmap != null) {
Log.d(TAG, "loadBitmapFromMemCache, url = " + url);
return bitmap;
}
try {
bitmap = loadBitmapFromDiskCache(url, reqWidth, reqHeight);
if (bitmap != null) {
Log.d(TAG, "loadBitmapFromDisk, url = " + url);
return bitmap;
}
bitmap = loadBitmapFromHttp(url, reqWidth, reqHeight);
Log.d(TAG, "loadBitmapFromHttp, url = " + url);
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap == null && !mIsDiskLruCacheCreated) {
Log.w(TAG, "encounter error, DiskLruCache is not created");
bitmap = downloadBitmapFromUrl(url);
}
return bitmap;
}
/**
*
* @param url
* @param imageView
* @param reqWidth
* @param reqHeight
*/
public void bindBitmap(final String url, final ImageView imageView, final int reqWidth, final int reqHeight) {
imageView.setTag(TAG_KEY_URL, url);
Bitmap bitmap = loadBitmapFromMemCache(url);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
return;
}
Runnable loadBitmapTask = new Runnable() {
@Override
public void run() {
Bitmap bitmap = loadBitmap(url, reqWidth, reqHeight);
if (bitmap != null) {
LoaderResult result = new LoaderResult(imageView, url, bitmap);
mMainHandler.obtainMessage(MSG_POST_RESULT, result).sendToTarget();
}
}
};
THREAD_POOL_EXECUTOR.execute(loadBitmapTask);
}
private Bitmap downloadBitmapFromUrl(String urlString) {
Bitmap bitmap = null;
HttpURLConnection urlConnection = null;
BufferedInputStream in = null;
try {
final URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream(), IO_BUFFER_SIZE);
bitmap = BitmapFactory.decodeStream(in);
} catch (IOException e) {
Log.e(TAG, "error in downloadBitmap: " + e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
MyUtils.close(in);
}
return bitmap;
}
public File getDiskCacheDir(Context context, String uniqueName) {
boolean externalStorageAvailable = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
String cachePath = null;
if (externalStorageAvailable) {
cachePath = context.getExternalCacheDir().getPath();
} else {
context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private long getUsableSpace(File path) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
return path.getUsableSpace();
}
final StatFs stats = new StatFs(path.getPath());
return stats.getBlockSizeLong() * stats.getAvailableBlocksLong();
}
private Bitmap loadBitmapFromMemCache(String url) {
String key = hashKeyFromUrl(url);
return mMemoryCache.get(key);
}
private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
private Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
private Bitmap loadBitmapFromHttp(String url, int reqWidth, int reqHeight) throws IOException {
if (Looper.myLooper() == Looper.getMainLooper()) {
throw new RuntimeException("cannot visit network from UI thread");
}
if (mDiskLruCache == null) {
return null;
}
String key = hashKeyFromUrl(url);
DiskLruCache.Editor editor = mDiskLruCache.edit(key);
if (editor != null) {
OutputStream outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
if (downloadUrlToStream(url, outputStream)) {
editor.commit();
} else {
editor.abort();
}
mDiskLruCache.flush();
}
return loadBitmapFromDiskCache(url, reqWidth, reqHeight);
}
private Bitmap loadBitmapFromDiskCache(String url, int reqWidth, int reqHeight) throws IOException {
if (Looper.myLooper() == Looper.getMainLooper()) {
Log.w(TAG, "load bitmap from UI Thread, it's not recommended");
}
if (mDiskLruCache == null) {
return null;
}
Bitmap bitmap = null;
String key = hashKeyFromUrl(url);
DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
if (snapshot != null) {
FileInputStream fileInputStream = (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX);
FileDescriptor fileDescriptor = fileInputStream.getFD();
bitmap = mImageResizer.decodeSampledBitmapFromFileDescriptor(fileDescriptor, reqWidth, reqHeight);
if (bitmap != null) {
addBitmapToMemoryCache(key, bitmap);
}
}
return bitmap;
}
public boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
HttpURLConnection urlConnection = null;
BufferedOutputStream out = null;
BufferedInputStream in = null;
try {
final URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream(), IO_BUFFER_SIZE);
out = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
return true;
} catch (IOException e) {
Log.e(TAG, "downloadBitmap failed" + e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
MyUtils.close(in);
MyUtils.close(out);
}
return false;
}
private String hashKeyFromUrl(String url) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(url.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(url.hashCode());
}
return cacheKey;
}
private String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
}