从 url 加载图像仅适用于最后一个图像视图

load images from url just work for last imageview

现在我正在尝试从 URL 制作图像加载器,如果它只有 1 个图像并且 ImageView 工作正常,但是当我做超过 1 个时,

图像已加载,但只设置了最后一个 ImageView

代码在这里

public class ImageLoader {
private final String TAG = ImageLoader.this.getClass().getSimpleName();

private static ImageLoader imageLoader;
private Context context = null;
private String url = "";
private Boolean isRound = false;
private Boolean isCircle = false;
private int rad = 0, holder = 0, widht = 0, color = 0;
private ImageView img = null;

public static ImageLoader init(Context context) {

    imageLoader = new ImageLoader();
    imageLoader.context = context;

    return imageLoader;
}

public ImageLoader load(String url) {
    imageLoader.url = url;

    return imageLoader;
}

public ImageLoader isRound(boolean bool) {
    imageLoader.isRound = bool;
    imageLoader.rad = 4;

    return imageLoader;
}

public ImageLoader isRound(boolean bool, int rad) {
    imageLoader.isRound = bool;
    imageLoader.rad = rad;

    return imageLoader;
}

public ImageLoader stroke(int color, int width) {
    imageLoader.color = ContextCompat.getColor(context, color);
    imageLoader.widht = width;

    return imageLoader;
}

public ImageLoader stroke(int width) {
    imageLoader.color = ContextCompat.getColor(context, R.color.c_000000);
    imageLoader.widht = width;

    return imageLoader;
}

public ImageLoader isCircle(boolean bool) {
    imageLoader.isCircle = bool;

    return imageLoader;
}

public ImageLoader holder(int holder) {
    imageLoader.holder = holder;

    return imageLoader;
}

public ImageLoader with(ImageView container) {

    imageLoader.img = container;

    return imageLoader;
}

public void run() {

    new GetImageAsync().execute();
}

private class GetImageAsync extends AsyncTask<Void, Bitmap, Bitmap> {
    URL url_value = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        if (imageLoader.holder != 0) {
            Bitmap bitHolder = BitmapFactory.decodeResource(imageLoader.context.getResources(), imageLoader.holder);
            if (!isRound) {
                imageLoader.img.setImageBitmap(bitHolder);
            } else {
                imageLoader.img.setImageBitmap(ImageUtil.getRoundedCornerBitmap(bitHolder, imageLoader.color, rad, imageLoader.widht, imageLoader.context));
            }


        } else {
            if (isRound) {
                imageLoader.img.setBackgroundResource(R.drawable.blank);
            } else {
                imageLoader.img.setBackgroundResource(R.color.c_white);
            }

        }
    }

    @Override
    protected Bitmap doInBackground(Void... voids) {
        Bitmap bitmap = null;
        try {
            url_value = new URL(imageLoader.url);
            bitmap = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            bitmap = null;
        }

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);


        if (bitmap == null) {
            Log.d(TAG, "onPostExecute: Bitmap null");
            return;
        } else {

            Log.d(TAG, "onPostExecute: Bitmap not null"+imageLoader.img.toString());
            Log.d(TAG, "onPostExecute: Bitmap not null"+bitmap.toString());
        }


        if (isRound) {
            imageLoader.img.setImageBitmap(ImageUtil.getRoundedCornerBitmap(bitmap, imageLoader.color, imageLoader.rad, imageLoader.widht, imageLoader.context));
        } else {
            imageLoader.img.setImageBitmap(bitmap);
        }
    }
}
}

不要在 ImageLoader class 中使用 Async class,而是在一些单独的 class 中使用它。调用 GetImageAsync().execute(),下载所有图像并在 post 执行此操作,在 imageview 上设置图像,如下所示:

ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, image);