user1163234
user1163234

Reputation: 2497

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

Upvotes: 0

Views: 1956

Answers (3)

Tey Kar Shiang
Tey Kar Shiang

Reputation: 1

I achieved this using "paddingLeft" and "paddingRight" on parent layout; while apply a negative value to padding and margins to the child layout / ImageView. "android:clipToPadding" flag must be false to prevent the clipping.

<LinearLayout
    ...
    android:paddingLeft="10sp"
    android:paddingRight="10sp"
    android:clipToPadding="false"
    ...>

    <ImageView
        android:layout_marginLeft="-10sp"
        android:layout_marginRight="-10sp"
        android:paddingRight="-10sp"
        android:paddingLeft="-10sp"
        ...>

Upvotes: 0

Onur
Onur

Reputation: 5625

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.

Upvotes: 0

Lampione
Lampione

Reputation: 1608

If you have a known max size for your images, you can set the width of your layout equal to the largest width of your images set, then take all centered. This worked for me:

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

<RelativeLayout
    android:layout_width="1920px"
    android:layout_height="fill_parent"
    android:layout_gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/your_image" />
</RelativeLayout>
</FrameLayout>

Where 1920px is the largest width of your images set. The result is acceptable, EDIT: Image is perfectly centered.

Upvotes: 0

Related Questions