Android ImageView scale smaller image to width with flexible height without cropping or distortion

后端 未结 8 1924
面向向阳花
面向向阳花 2020-12-04 10:55

Often asked, never answered (at least not in a reproducible way).

I have an image view with an image that is smaller than the view. I want to scale

8条回答
  •  暖寄归人
    2020-12-04 11:23

      android:scaleType="fitCenter"
    

    Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.

    edit:

    The problem here is that the layout_height="wrap_content" is not "allowing" the image to expand. You'll have to set a size for it, for that change

      android:layout_height="wrap_content"
    

    to

      android:layout_height="100dp"  // or whatever size you want it to be
    

    edit2:

    works fine:

    
    

提交回复
热议问题