我想从我的SD卡加载一些图像并将它们设置为我的应用程序布局的背景。我想有效地做到这一点,所以我没有得到OutOfMemory Exception并在后台执行。所以我在Processing Bitmaps中阅读并使用了几乎有关使用位图的确切代码 在Android开发人员。这是我的代码:
public class ImageLoader {
private final Context context;
private int imageWidth;
private int imageHeight;
public ImageLoader(Context context, int imageWidth, int imageHeight) {
this.context = context;
setImageSize(imageWidth, imageHeight);
}
public ImageLoader(Context context, int imageSize) {
this.context = context;
setImageSize(imageSize);
}
public void setImageSize(int width, int height) {
imageWidth = width;
imageHeight = height;
}
public void setImageSize(int size) {
setImageSize(size, size);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampleBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static boolean cancelPotentialWork(int data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final int bitmapData = bitmapWorkerTask.data;
if (bitmapData != data) {
// Cancel previous task
bitmapWorkerTask.cancel(true);
} else {
// The same work is already in progress
return false;
}
}
// No task associated with the ImageView, or an existing task was cancelled
return true;
}
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
public void loadBitmap(int resId, ImageView imageView) {
if (cancelPotentialWork(resId, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable =
new AsyncDrawable(context.getResources(), ???mPlaceHoderBitmap???, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(resId);
}
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampleBitmapFromResource(context.getResources(), data, imageWidth, imageHeight);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
final BitmapWorkerTask bitmapWorkerTask =
getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask && imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap,
BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference =
new WeakReference<ImageLoader.BitmapWorkerTask>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
}
但是在loadBitmap(int resId,Bitmap位图,ImageView imageView)方法中我不知道如何传递位图。如果我使用BitmapFactory.decode *我可能会得到异常。我应该如何为我的图像数据源传递位图参数?
答案 0 :(得分:1)
在那篇文章中:
public void loadBitmap(int resId, ImageView imageView) {
if (cancelPotentialWork(resId, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable =
new AsyncDrawable(getResources(), mPlaceHolderBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(resId);
}
}
在传递实际图像时,传递给AsyncDrawable
的位图应该是占位符。
所以你可以解码占位符图像一次,然后传递它。
在Activity中创建一个Bitmap对象:
Bitmap placeHolder=null;
并在onCreate
placeHolder=BitmapFactory.decodeResource(getResources(), R.drawable.place_holder);
现在每次要从SD卡调用中加载一些图像时:
loadBitmap(<resId>,placeHolder,imageView);