If you’re anything like me, you are really, really, ridiculously good
looking. But you’re also probably a tad forgetful. So when it comes time
to scale an image in an ImageView
, you cannot for the life of you remember what all the different ScaleType
s
actually look like on the screen. So, you spend the next 10-15 minutes
building and rebuilding your app with each and every scale type to see what
they all look like. Then you inevitably forget the difference between two of
them and start the whole process all over again. As the kids
say, “I gotchu fam”.
Below are screenshots of all the different ScaleType
s placed side-by-side.
And below that are all the ScaleType
definitions copy and pasted directly
from the official Android docs. And even further below those is a helpful tip
for those brave souls who make it to the end of
this post :)
Scale Types
The full descriptions of each ScaleType
from the official Android documentation
CENTER
Center the image in the view, but perform no scaling.
CENTER_CROP
Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
CENTER_INSIDE
Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
FIT_CENTER
Scale the image using Matrix.ScaleToFit.CENTER
Matrix.ScaleToFit.CENTER
: 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.
FIT_END
Scale the image using Matrix.ScaleToFit.END
Matrix.ScaleToFit.END
: 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. END aligns the result to the right and bottom edges of dst.
FIT_START
Scale the image using Matrix.ScaleToFit.START
Matrix.ScaleToFit.START
: 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. START aligns the result to the left and top edges of dst.
FIT_XY
Scale the image using Matrix.ScaleToFit.FILL
Matrix.ScaleToFit.FILL
: Scale in X and Y independently, so that src matches dst exactly. This
may change the aspect ratio of the src.
MATRIX
Scale using the image matrix when drawing.
Adjust View Bounds
While not technically an ImageView.ScaleType
this will come in handy. If you
notice with CENTER_INSIDE
, FIT_CENTER
, FIT_END
and FIT_START
the
actual bounds of the ImageView
are much larger than the scaled image. To set
the bounds of the ImageView
to the height of the image inside, use
android:adjustViewBounds="true”
in your XML. It looks like this: