For example, there is a list in which item s have text fields and n-th number of pictures. What am I doing:

1) I use the ImageLoader library

In the Application class, I initialize it as follows.

 public static void initImageLoader(Context context) { DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(null) .cacheInMemory(false) .cacheOnDisk(true) .considerExifParams(true) .showStubImage(R.drawable.place_holder_view_white) .build(); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .diskCacheSize(50 * 1024 * 1024) .tasksProcessingOrder(QueueProcessingType.LIFO) .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) .defaultDisplayImageOptions(options) .build(); ImageLoader.getInstance().init(config); } 

Where .showStubImage(R.drawable.place_holder_view_white) picture until a loaded picchi appears. This is just a white png-box, with specific dimensions. In the project she is alone.

2) In the adapter, I invoke the boot method, which is:

 ImageLoader.getInstance().displayImage(url, imageView, new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { ImageLoader.getInstance().displayImage("drawable://" + R.drawable.icon_image_holder, imageView); ObjectAnimator.ofFloat(imageView, "alpha", 0, 1).setDuration(400).start(); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { ObjectAnimator.ofFloat(imageView, "alpha", 0, 1).setDuration(400).start(); } @Override public void onLoadingCancelled(String imageUri, View view) { } }); 

Here you can see that if the picture is not loaded, then it is replaced with another one taken from the project. And if it is loaded, it is smoothly displayed on the place of the stub, through aplha animation.

Everything works fine, but! only if all the pictures have the same size, which coincides with the size of the stub picture. Otherwise, the list starts to twitch. How to make imageHolder automatically adjust to the future size of the inserted image?

  • one
    Well, either you need to know in advance the dimensions of the future image and insert the stubs with scaleType necessary (centerCrop for example), or set the size of the image to the stub size (i.e., without wrap_content) and also enter the downloaded pictures with the scaleType ... -
  • I agree, but according to me the first solution is unreal / cumbersome, but the second one is quite acceptable - Android Android
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

You can make ImageLoader adjust the size of the loaded image in the preprocessor.

Create your BitmapProcessor

  class MyBitmapProcessor implements BitmapProcessor{ private int maxSize; private MyBitmapProcessor(){} public MyBitmapProcessor(int size){ maxSize = size; } @Override public Bitmap process(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); if (width > height) { float ratio = (float) width / maxSize; width = maxSize; height = (int) (height / ratio); } else if (height > width) { float ratio = (float) height / maxSize; height = maxSize; width = (int) (width / ratio); } else { height = maxSize; width = maxSize; } return Bitmap.createScaledBitmap(bitmap, width, height, true); } } 

and we set it in the loader option

  MyBitmapProcessor bitmapProcessor = new MyBitmapProcessor(200); new DisplayImageOptions.Builder() .preProcessor(bitmapProcessor) ... 

You can also try to use the display with animation.

  new DisplayImageOptions.Builder() .displayer(new FadeInBitmapDisplayer(300)) ... 
  • Thank you for the answer, but I am not aware of the size. - Android Android