设置ImageView自动高度取决于图片宽度
Set ImageView auto height depends on the image width
有没有办法根据宽度在 ImageView 上设置自动高度?例如在 Javascript 中,如果您在图像上设置宽度,它会计算图像的高度 auto
我的应用程序从服务器加载图像,这取决于用户的偏好。它可以是 200dp x 50dp 大小或 50dp x 50dp
我正在使用 Android 图片加载器。如果我在 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 有一个代码片段
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
}
});
这将加载图像,将其解码为位图和 return 位图以进行回调
在这里你可以使用 targetSize
设置高度宽度
有没有办法根据宽度在 ImageView 上设置自动高度?例如在 Javascript 中,如果您在图像上设置宽度,它会计算图像的高度 auto
我的应用程序从服务器加载图像,这取决于用户的偏好。它可以是 200dp x 50dp 大小或 50dp x 50dp
我正在使用 Android 图片加载器。如果我在 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 有一个代码片段
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
}
});
这将加载图像,将其解码为位图和 return 位图以进行回调 在这里你可以使用 targetSize
设置高度宽度