14

I have an image that is 480 pixels by 40 pixels. I would like to show this image in an ImageView without having it scale to fit.

Ex: If my screen is only 320pixels wide I would like only 320pixels of the image to show on the screen, not have it cram itself into the ImageView (even if it means that the rest of the image gets cut off).

Is there a way to prevent it from scaling to fit?

EDIT: Also, I would like the left side of the image to line up with the left side of the device's screen.

Thanks

PaulG
  • 6,920
  • 12
  • 54
  • 98

6 Answers6

29

Use ScaleType.CENTER, like this in XML:

android:scaleType="center"

This will "center the image in the view, but perform no scaling"

Read more here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

(This is by the way the first hit on googling "imageview scale")

Michell Bak
  • 13,182
  • 11
  • 64
  • 121
nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • `ScaleType` is definitely the way to go. Works for a lot of different use cases. – Michell Bak Feb 15 '12 at 15:54
  • Thanks, but is there a way to have it stick to the left? – PaulG Feb 15 '12 at 15:54
  • @PaulG depending on what you mean, but if you want the left part of your image to be displayed and, as there is more space, the rightmost part to be displayed.. Then, there is no solution inside the SDK, you will have to check the source code of ImageView and create your own transformation matrix. – Snicolas Jun 06 '13 at 07:36
  • Couldn't figure out why it wasn't working for me, until I found that I was using `background` instead of `src` to specify the drawable :-s – Divisible by Zero Oct 02 '14 at 08:36
16

Use the MATRIX scaletype to set it for no scaling and no centering

It will use the identity matrix unless you set it using setImageMatrix(matrix);

android:scaleType="matrix"

Or in Java:

view.setScaleType(ImageView.ScaleType.MATRIX);
Renate
  • 761
  • 4
  • 11
  • Thanks a lot! scaleType center didn't work as expected but matrix did. In my case i have a TextView layout_toLeftOf the image and when the text grows I need the image at the right to stay always with the original size. – Moxor May 18 '15 at 13:53
  • Thanks, this is exactly what I needed to debug an image map -based UI, detecting hits was buggy until I set the scaleType to matrix. – slogan621 Mar 29 '19 at 07:09
3

Try this one:

                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
Kai Wang
  • 3,303
  • 1
  • 31
  • 27
1

If

android:scaleType="center"

doesn't work, check how do you set your image. You have to use android:src, not android:backgorund

so

 android:scaleType="center"
 android:src="@drawable/yourDrawable"
Jakub S.
  • 5,580
  • 2
  • 42
  • 37
0

I had a similar issue where I had a fixed size graphic that I wanted to scale to the screen but stay in proportion it was 480 x 200.

I used Fill_parent on the height so it would scale and wrap_content on the width. As well as Align Parent Left and Align Parent Top.

In your case you could just use wrap_content on both as well as Align Parent Left. Then I tweeked the width using Right Margin 180dp on normal screens to be sure it maintained the width.

<ImageView
    android:id="@+id/imageLayer"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="180dp"
    tools:ignore="ContentDescription" />
birdman
  • 1,134
  • 13
  • 13
0

My Problem was that I set the Scaletype in the xml-layout-file, through I set the picture manually in Java-Code.
The Solution was to set the scale-type in Java-Code instead the XML-file.

Here is what has worked for me:

// set image
backgroundImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Samuel
  • 356
  • 2
  • 13