gpt4 book ai didi

android - 设置ImageView自动高度取决于图片宽度

转载 作者:行者123 更新时间:2023-11-29 19:50:35 25 4
gpt4 key购买 nike

有没有办法根据宽度在 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"但没有任何效果

-------- 已编辑------------

我结束了自己做,这可能会帮助其他人:

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

感谢 Muhammad Hamza Shahid 的帮助

最佳答案

如果您查看库自述文件的完整部分 Android-Universal-Image-Loader有一个代码 fragment

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 设置高度宽度

关于android - 设置ImageView自动高度取决于图片宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com