Search code examples
androidandroid-imageviewaspect-ratioandroid-bitmapion-koush

Resizing image to fill width of screen


I am trying to set an Image that I am downloading from the web. I am using a library to help me do this called ion. The problem is that I need to resize the image to fit my screen and keep the aspect ration. The height seems to be working fine however I want to set the width of the bitmap to fit the whole width of the devicewhile keeping the aspect ratio. But it is not working and the image is cut short on both sides...

  <ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/_height" />

Code:

DisplayMetrics metrics = getResources().getDisplayMetrics();
mWidth = metrics.widthPixels;
mHieght = (int) getResources().getDimension(R.dimen._height);
Ion.with(imageView).resize(mWidth, mHieght).load(url)

enter image description here

Edit: This is with android:scaleType="centerCrop"

enter image description here


Solution

  • ImageView has scale type feature. Check ImageView.ScaleType. I think centerInside or fitCenter is what you are looking for.

    Update

    Sorry didn't realize you have a fixed height at first. centerCrop should be the answer.