3

I want to put and ImageView with a large Y margin on my screen device, which would imply that part of the image would be out of screen, and then that the image would be cropped.

The problem is that Android is scaling the image all the time, so that it fits inside the screen, but I don't want that, I want the image to be cropped.

How can I force the ImageView to be cropped and not resized?

P.S. I tried all the possible ScaleType properties and none of them worked for me!

Code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    android:background="@drawable/my_image"
    android:scaleType="centerCrop"
    />      

</RelativeLayout>
2
  • Wait. CENTER_CROP should keep the image the normal size without any scaling. Post the code please.
    – DeeV
    Aug 7, 2012 at 19:49
  • I just added some code to my question. CENTER_CROP doesn't work for me.
    – thomaus
    Aug 7, 2012 at 20:01

2 Answers 2

4

Try replacing android:background with android:src.

2
  • Thanks. The bottom of the image is correctly cropped but the top of the image is also cropped which is strange.
    – thomaus
    Aug 7, 2012 at 20:33
  • This is the expected behavior for centerCrop. For what you are trying to do, this post may help you: "I think it should work to put it inside a frame layout with exact values for width/height (specifying maximum image size before cropping occurs). Then on the image view, specify layout_gravity=right, and width/height as wrap_content."
    – J.-B. C.
    Aug 7, 2012 at 20:46
3

First problem with your code is that you used:

android:background="@drawable/my_image"

instead of:

android:src="@drawable/my_image"

(with background none of the scaleType options work).

Now if this still doesn't help, you probably have to use scaleType="matrix" and then simply create a matrix that will do the required job. For example, let's assume that you want to:

  • keep the ratio of your image
  • scale the image so that the width parameter will equal X (for example: X can be the width of the screen)
  • make the top of the image visible (so crop the bottom of the image) - I'm assuming this is why centerCrop might not work for you

Here's the code:

ImageView imgView = (ImageView)findViewById(R.id.my_image_view);    
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
    R.drawable.my_image);

Matrix matrix = new Matrix();
// Let's assume X = 400
float scale = ((float) 400) / bitmap.getWidth();
matrix.setScale(scale, scale);

imgView.setImageMatrix(matrix);

And remember to make the necessary changes in the xml file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.