我正在尝试实现一个图像库,它应该显示〜 5-15 个较小的图像和一个“当前选择的”更大的图像。
它看起来像:http ://www.mobisoftinfotech.com/blog/wp-content/uploads/2012/06/galleryDemo.png
我查了很多资源,现在决定使用位图缓存(lru-cache)(感谢这个论坛的人!)。
我目前没有内存泄漏,但我对这个解决方案不满意,因为每次滚动时,都会从缓存中删除一些图像,我必须重新加载它们......所以用户必须等待重新加载图像......每次滚动到另一边时等待 0.5 - 1 秒真的很烦人......
public static WeakReference<Bitmap> getBitmap(String imageName, int width,
int height) {
if (!imageName.contains(".jpg")){
imageName += ".jpg";
}
String pathToImage = getPathToImage(imageName);
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathToImage, options);
/*
* Calculate inSampleSize
*/
options.inSampleSize = calculateInSampleSize(options, width, height);
/*
* Removes the alpha-channel from bitmap (not necessary)
*/
options.inPreferredConfig = Bitmap.Config.RGB_565;
/*
* Decode bitmap with inSampleSize set
*/
options.inJustDecodeBounds = false;
WeakReference<Bitmap> scaledBitmap = new WeakReference<Bitmap>(
BitmapFactory.decodeFile(pathToImage, options));
return scaledBitmap;
我是否有可能在获取图像时出错?目前,我将为单选图像使用 320x480 分辨率,为底部列表使用 64x64 分辨率...
真的很奇怪,不管图像的分辨率有多大,lru-cache 仍然会删除一些图像,即使我只选择 64x64 图像......我可能做错了什么lru缓存?
以下代码显示了我的实现:
/*
* Bitmaps which should be shown
*/
mMemoryCache = (BitmapCache) getLastNonConfigurationInstance();
if (mMemoryCache == null) {
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/4th of the available memory for this memory cache.
final int cacheSize = maxMemory / 2; // TODO default 8
mMemoryCache = new BitmapCache(cacheSize);
}
和类位图缓存:
public BitmapCache(int maxSize, Resources resources) {
super(maxSize);
this.resources = resources;
}
@Override
protected int sizeOf(String key, AsyncDrawable value) {
if (value.getBitmap() != null) {
/*
* The cache size will be measured in kilobytes rather than
* number of items.
*/
return value.getBitmap().getRowBytes()
* value.getBitmap().getHeight() / 1024;
}
return super.sizeOf(key, value);
}
public void loadBitmap(Picture picture, ImageView mImageView,
ImageResolution resolution) {
if (cancelPotentialWork(picture.getFileName(), // + resolution.name()
mImageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(this,
mImageView, picture, resolution);
final AsyncDrawable asyncDrawable = new AsyncDrawable(resources,
null, task);
mImageView.setImageDrawable(asyncDrawable);
task.execute();
}
}
非常感谢你 :(
更多代码:BitmapWorkerTask:
@Override
protected Bitmap doInBackground(Void... params) {
/*
* Decode image in background.
*/
final Bitmap bitmap = FileHandler.getBitmap(
picture.getPictureId() + "", resolution).get();
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
/*
* Once complete, see if ImageView is still around and set bitmap.
*/
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
final BitmapWorkerTask bitmapWorkerTask = asyncDrawable
.getBitmapWorkerTask();
if (this == bitmapWorkerTask && imageView != null) {
imageView.setImageBitmap(bitmap);
bitmapCache.addBitmapToMemoryCache(
picture.getFileName(), // + resolution.name()
asyncDrawable);
}
}
}
}
}