CQM
CQM

Reputation: 44228

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?

Upvotes: 4

Views: 17110

Answers (3)

Antrromet
Antrromet

Reputation: 15414

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

Upvotes: 15

radley
radley

Reputation: 3572

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter" />

It's a basic setting.

Upvotes: 2

Steven Byle
Steven Byle

Reputation: 13269

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?

Upvotes: 0

Related Questions