现在我正在尝试从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);
}
}
}
}
答案 0 :(得分:0)
不要在ImageLoader类中使用Async类,而是在某个单独的类中使用它。调用GetImageAsync()。execute(),下载所有图像&amp;在执行此操作后,在imageview上设置图像,如下所示:
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, image);