我正在尝试构建像Android原生图片库这样的图片库。 我正在使用CursorAdapter,带有磁盘缓存和Mem缓存,我将所有图像调整到最小尺寸,还尝试了BitmapFactory的inBitamp选项,似乎没有什么能像普通图库一样快速和平滑地加载
这是Runnable,它主要完成所有加载。
/**
* Class to asynchronously perform the loading of the bitmap
*/
public class ImageCalcInSampleRunnable implements Runnable {
protected ImageLoadingArgs mArgs = null;
public ImageLoadingArgs getmArgs() {
return mArgs;
}
public ImageCalcInSampleRunnable(ImageLoadingArgs args) {
mArgs = args;
}
Future mMyTask = null;
long timeCounter = 0;
public void run() {
timeCounter = System.currentTimeMillis();
if (mArgs.position < mFirstVisibleItem - IMAGE_CANCEL_THERSHOLD
||
mArgs.position > mLastVisibleCount + IMAGE_CANCEL_THERSHOLD) {
if (firstReturn) {
firstReturn = false;
}
return;
}
firstReturn = true;
boolean ismimeTypeImage = true;
if (mArgs.mMimeTpe != null && mArgs.mMimeTpe.contains("video")) {
ismimeTypeImage = false;
}
if (mDiskCache.containsKey(mArgs.mImageId)) {
mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);
addBitmapToMemoryCache(ContentUris.parseId(mArgs.mUri), mArgs.mBitmap);
if (mArgs.mBitmap != null)
if (!ismimeTypeImage)
mArgs.isVideo = true;
} else {
if (ismimeTypeImage) {
try {
mArgs.mBitmap = MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, Long.parseLong(mArgs.mImageId),
MediaStore.Images.Thumbnails.MINI_KIND, null);
mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, false);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (mArgs.mBitmap == null) {
mArgs.options = ImageManager.decodeInSampledBitmapFromResourceMulti(mArgs.mData, mArgs.mWidth, mArgs.mheight);
if (Utils.hasHoneycomb()) {
mDiskCache.addInBitmapOptions(mArgs.options);
mArgs.mBitmap = BitmapFactory.decodeFile(mArgs.mData, mArgs.options);
}else {
mArgs.mBitmap = BitmapFactory.decodeFile(mArgs.mData, mArgs.options);
}
mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, false);
}
if (mArgs.mBitmap != null) {
AddBitmapToDiskCache(mArgs.mImageId, mArgs.mBitmap);
if (mDiskCache.containsKey(mArgs.mImageId)) {
mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);
}
addBitmapToMemoryCache(ContentUris.parseId(mArgs.mUri), mArgs.mBitmap);
} else {
try {
mArgs.mBitmap = MediaStore.Video.Thumbnails.getThumbnail(mContentResolver, Long.parseLong(mArgs.mImageId),
MediaStore.Video.Thumbnails.MINI_KIND, null);
mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, true);
} catch (NumberFormatException e) {
}
if (mArgs.mBitmap != null) {
mArgs.isVideo = true;
AddBitmapToDiskCache(mArgs.mImageId, mArgs.mBitmap);
if (mDiskCache.containsKey(mArgs.mImageId)) {
mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);
}
addBitmapToMemoryCache(Long.parseLong(mArgs.mImageId), mArgs.mBitmap);
}
}
} else {
try {
mArgs.mBitmap = MediaStore.Video.Thumbnails.getThumbnail(mContentResolver, Long.parseLong(mArgs.mImageId),
MediaStore.Video.Thumbnails.MINI_KIND, null);
mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, true);
} catch (NumberFormatException e) {
}
if (mArgs.mBitmap != null) {
mArgs.isVideo = true;
AddBitmapToDiskCache(mArgs.mImageId, mArgs.mBitmap);
if (mDiskCache.containsKey(mArgs.mImageId)) {
mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);
}
addBitmapToMemoryCache(Long.parseLong(mArgs.mImageId), mArgs.mBitmap);
}
}
}
if (mArgs.position > mFirstVisibleItem - IMAGE_CANCEL_THERSHOLD
&&
mArgs.position < mLastVisibleCount + IMAGE_CANCEL_THERSHOLD) {
mArgs.mHandler.sendMessage(mArgs.mHandler.obtainMessage(1,mArgs));
}
Log.d("shimi5", "timecount=" + (System.currentTimeMillis() - timeCounter));
timeCounter = (System.currentTimeMillis() - timeCounter);
if (timeCounter < 45) {
try {
Thread.sleep(45 - timeCounter);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void cancel() {
}
@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof ImageCalcInSampleRunnable) {
if (mArgs.mUri != null) {
return mArgs.mUri.equals(((ImageCalcInSampleRunnable) obj).mArgs);
}
}
return false;
}
@Override
public int hashCode() {
return mArgs.mUri.hashCode();
}
}
在此之后,处理程序只需将setImageBitmap作为图像视图。 任何帮助都非常有帮助
感谢。
答案 0 :(得分:1)
在这个问题上一段时间后,我放弃了尝试优化代码,以便更清晰,更流畅地加载图像。
我发现了这个: https://github.com/georgiecasey/android-gallery-library
这是Android开源图库ICS的一个分支。
在此找到了很多见解。