How do I make my ImageView a fixed size regardless of the size of the bitmap(无论位图大小如何,如何使我的 ImageView 成为固定大小)
问题描述
所以我从 Web 服务加载图像,但图像的大小有时比其他图像更小或更大,当我将它们放在 android 的 ListView 中时,可视化看起来很傻.我想修复我的 ImageView 的大小,以便它只显示图像的一部分,如果它大于预设数量.我已经尝试了所有我能想到的设置 setMaxWidth/setMaxHeight,将比例类型设置为 centerCrop,使用 ClipableDrawable 包装我的 BitmapDrawable,使用 Drawable.setBounds() 设置.我尝试过在 XML 中进行设置并以编程方式进行设置,但都没有奏效.我很惊讶设置最大宽度/高度没有做任何事情.下面是我在布局文件中使用 ImageView 定义的 XML:
So I'm loading images from a web service, but the size of the images are sometimes smaller or bigger than other images and the visualization looks silly when I put them in a ListView in android. I'd like to fix the size of my ImageView so that it only shows a portion of the image if it's larger than a preset amount. I've tried everything I can think of setting the setMaxWidth/setMaxHeight, setting the scale type to centerCrop, using ClipableDrawable wrapping my BitmapDrawable, setting using Drawable.setBounds(). I've tried setting in the XML and programmatically, but neither worked. I'm very surprised setting max width/height didn't do anything. Below is the XML I'm using ImageView definition in my layout file:
<ImageView android:id="@+id/recipeImage"
android:maxHeight="64px"
android:maxWidth="64px"
android:scaleType="centerCrop"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"/>
但是,没有任何效果.
推荐答案
只需删除 maxHeight
和 maxWidth
属性并在 layout_width
和 layout_height
适当.但不要使用 px
单元 - 使用 dp
代替,因为这些是设备无关的.
Just delete the maxHeight
and maxWidth
attributes and enter your units in layout_width
and layout_height
appropriately. But do not use the px
unit - use dp
instead, because these are device-independent.
这篇关于无论位图大小如何,如何使我的 ImageView 成为固定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无论位图大小如何,如何使我的 ImageView 成为固定大小


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01