Search code examples
androidimageviewuniversal-image-loader

Set ImageView auto height depends on the image width


Is There a way to set an auto height on an ImageView depends on the width? For example in Javascript, if you set width on an image, it calculate the image's height auto

My app loads image from the server, depends on the user's preferences. It can be an 200dp x 50dp size or 50dp x 50dp

I'm using Android Image Loader. If I set on the ImageView wrap_content for width and height, it shows nothing

This is my UIL default config:

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisk(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .build();

ImageLoaderConfiguration defaultConfiguration =
            new ImageLoaderConfiguration.Builder(getApplicationContext())
                    .threadPriority(Thread.NORM_PRIORITY - 2)
                    .defaultDisplayImageOptions(defaultOptions)
                    .diskCacheSize(100 * 1024 * 1024)
                    .denyCacheImageMultipleSizesInMemory()
                    .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                    .tasksProcessingOrder(QueueProcessingType.LIFO)
                    .build();

ImageLoader.getInstance().init(defaultConfiguration);

And for the image:

new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .showImageForEmptyUri(R.drawable.ic_user_default)
            .showImageOnFail(R.drawable.ic_user_default)
            .showImageOnLoading(R.drawable.ic_user_default)
            .build();

I tried using android:adjustViewBounds="true", android:scaleType="centerCrop" and android:scaleType="fitXY" but nothing works

------- EDITED ------------

I ended doing by myself, this maybe help other:

  imageLoader.displayImage(logoUrl, logoPhoto, options2, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // Max dimension for logo images
                int maxWidth = 879, maxHeight = 400;
                // Get the correct dp scale measure
                final float scale = getResources().getDisplayMetrics().density;
                // Width of the image donwloaded
                int width = loadedImage.getWidth();
                // Height of the image donwloaded
                int height = loadedImage.getHeight();
                // Convert image width px on dp
                int widthInDp = (int) (width * scale);
                // Convert image height px on dp
                int heightInDp = (int) (height * scale);

                Log.d(TAG, String.format("Original image size: %s, %s", width, height));
                Log.d(TAG, String.format("Original image size in DP: %s, %s", widthInDp, heightInDp));
                if (heightInDp > maxHeight) {
                    double reductionPercentage = ((maxHeight * 100) / heightInDp);

                    widthInDp = (int) (widthInDp * (reductionPercentage / 100));
                    heightInDp = (int) (heightInDp * (reductionPercentage / 100));

                    Log.d(TAG, String.format("Height | Recalculating height and width: %s, %s", widthInDp, heightInDp));

                }

                if (widthInDp > maxWidth) {
                    double reductionPercentage = ((maxWidth * 100) / widthInDp);

                    widthInDp = (int) (widthInDp * (reductionPercentage / 100));
                    heightInDp = (int) (heightInDp * (reductionPercentage / 100));

                    Log.d(TAG, String.format("Width | Recalculating height and width: %s, %s", widthInDp, heightInDp));
                }

                logoPhoto.getLayoutParams().width = widthInDp;
                logoPhoto.getLayoutParams().height = heightInDp;
            }
        });

Thanks to Muhammad Hamza Shahid for the help


Solution

  • If you look at the Complete section of Readme of the library Android-Universal-Image-Loader there is a code snipet

    ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit   to this size
    imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
    });
    

    this will load image, decode it to Bitmap and return Bitmap to callback and here you can set height width using targetSize