Java 无论位图的大小如何,如何使我的 ImageView 成为固定大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3781790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:56:22  来源:igfitidea点击:

How do I make my ImageView a fixed size regardless of the size of the bitmap

javaandroidbitmapimageview

提问by chubbsondubs

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:

所以我从网络服务加载图像,但图像的大小有时比其他图像更小或更大,当我将它们放在 android 的 ListView 中时,可视化看起来很傻。我想修复我的 ImageView 的大小,以便它只显示图像的一部分,如果它大于预设数量。我已经尝试了所有我能想到的设置 setMaxWidth/setMaxHeight,将比例类型设置为 centerCrop,使用 ClipableDrawable 包装我的 BitmapDrawable,使用 Drawable.setBounds() 进行设置。我试过在 XML 和编程中进行设置,但都没有奏效。我很惊讶设置最大宽度/高度没有做任何事情。下面是我在布局文件中使用 ImageView 定义的 XML:

    <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"/>

But,nothing has worked.

但是,没有任何效果。

采纳答案by mreichelt

Just delete the maxHeightand maxWidthattributes and enter your units in layout_widthand layout_heightappropriately. But do not use the pxunit - use dpinstead, because these are device-independent.

只需删除maxHeightmaxWidth属性layout_widthlayout_height适当地输入您的单位。但不要使用该px单元 -dp而是使用,因为这些与设备无关。

回答by Krishh

This will fit the image in the specified height and width, irrespective of image size.

无论图像大小如何,这都将适合指定高度和宽度的图像。

<ImageView
        android:id="@+id/image5"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:src="@drawable/image_1"
        android:scaleType="fitXY"/>

回答by Vikash Sharma

Get ImageVIews as facebook posts.    
Glide.with(context).load(link)
                    .placeholder(R.drawable.aboutlogo)
                    .fitCenter()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageView);`

        <ImageView
                    android:id="@+id/img1"
                    android:src="@drawable/ic_menu_camera"
                    android:adjustViewBounds="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="center"/>