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

我怕爱的太早我们不能终老 提交于 2019-11-30 12:28:17

问题


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

But,nothing has worked.


回答1:


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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/3781790/how-do-i-make-my-imageview-a-fixed-size-regardless-of-the-size-of-the-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!