简体   繁体   中英

Android imageview, scale full screen, and centerCrop

How can I have an imageview whose image fills the screen, but maintains its aspect ratio, and fits in the center?

Currently I have large images, larger than phone screen resolution, but I suspect that they will be be letterboxed on devices with higher resolution

What combination of scaleType will do the trick?

You could simply use adjustViewBounds value to maintain the aspect ratio of the image

<ImageView
        android:id="@+id/metallicaImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:src="@drawable/metallica" />
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter" />

It's a basic setting.

I've run into this before, the issue is that when the image needs to be scaled up (screen width is larger than the image), Android won't scale the image beyond its given size. However, scaling down works just fine.

I ended up creating the AspectRatioImageView mentioned below, works well. Be sure to read the comments about getIntrinsicWidth() returning 0 and getDrawable() returning null, and put in some safety checks for those.

How to stretch three images across the screen preserving aspect ratio?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM