我正在開發全屏圖庫,並且正在開發自定義適配器。 我想先爲應用程序啓動並滾動視圖(圖像)從文件路徑加載位圖來顯示我的所有視圖的佔位符圖像。我使用Loading bitmaps efficiantly教程加載位圖。設置自定義全屏適配器的位置持有者
我的問題是,placeHolder圖像只顯示在應用程序中,它不會加載其他圖像。
我試圖在我的AsyncTask類中的doInBackground和onPostExecute等不同位置設置mPlaceHolder位圖,但它並沒有幫助我。
我錯過了什麼嗎?
這裏是我的適配器代碼:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setAdjustViewBounds(true);
imageView.setPadding(20, 0, 20, 0);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
try {
loadBitmap(imagePaths.get(position), imageView);
} catch (Exception e) {
// TODO: handle exception
}
//Bitmap bitmap =decodeFiles(imagePaths.get(position),width,height);
/*
try{
image = decodeFile(new File(imagePaths.get(position)),screenMaxSize);
}
catch (Exception e) {
// TODO: handle exception
Log.i("erroe", imagePaths.get(position));
}
*/
//imageView.setImageBitmap(bitmap);
//imageView.setLayoutParams(new GridView.LayoutParams(90, 70));
return imageView;
}
public void loadBitmap(String filePath, ImageView imageView) {
if (cancelPotentialWork(filePath, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final Bitmap *mPlaceHolderBitmap* = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gallery_icon);
final AsyncDrawable asyncDrawable =
new AsyncDrawable(mContext.getResources(), mPlaceHolderBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(filePath);
}
}
public Bitmap decodeFiles(String pathName, int reqWidth,int reqHeight){
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
/*
int scale = 1;
if (o.outHeight > screenMaxSize || o.outWidth > screenMaxSize) {
scale = (int)Math.pow(2, (int) Math.round(Math.log(screenMaxSize/(double) Math.max(o.outHeight, o.outWidth))/Math.log(0.5)));
}
*/
//Decode with inSampleSize
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, options);
}
private int calculateInSampleSize(Options options, int reqWidth, int reqHeight) {
// TODO Auto-generated method stub
// 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 boolean cancelPotentialWork(String filePath, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final String bitmapData = bitmapWorkerTask.data;
if (bitmapData != filePath) {
// 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;
}
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private String data = "";
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(String... params) {
data = params[0];
return decodeFiles(data, width, height);
}
// Once complete, see if ImageView is still around and set bitmap.
@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<BitmapWorkerTask>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
Tnx爲答案,但我只看到我的所有圖像的第一個初始位圖:-( –