设置ImageView自动高度取决于图像宽度

时间:2016-05-12 03:51:28

标签: android imageview universal-image-loader

有没有办法在ImageView上设置自动高度取决于宽度?例如,在Javascript中,如果您在图像上设置宽度,则会计算图像的高度自动

我的应用从服务器加载图片,取决于用户的偏好。它可以是200dp x 50dp大小或50dp x 50dp

我正在使用Android Image Loader。如果我在ImageView wrap_content上设置宽度和高度,则不显示任何内容

这是我的UIL默认配置:

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);

对于图像:

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();

我尝试使用android:adjustViewBounds="true"android:scaleType="centerCrop"android:scaleType="fitXY",但没有任何作用

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

我自己结束了,这可能有助于其他人:

  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;
            }
        });

感谢穆罕默德·哈姆扎·沙希德的帮助

1 个答案:

答案 0 :(得分:2)

如果您查看库Android-Universal-Image-Loader的自述文件的完整部分,则会有代码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
}
});

这将加载图像,将其解码为Bitmap并返回Bitmap以进行回调 在这里你可以使用targetSize

设置高度宽度