dev90
dev90

Reputation: 7519

how to stretch image without disturbing the aspect ratio of image in Android?

I have created a view pager, It has a images, that comes from server. The resolution of the image is 980X551.

I need to display this image in half of the screen.

In ViewPage Layout, I set the widthXheight of image to match_parent.

and in my MainActivity, I use weight to define the height of ViewPager.

The problem is that, In my Main Activity when i increase the height of image, its start stretching, and i need to show this in half Page., and it looked quite stretch when i set its height to half screen.

Kindly guide me how to stretch image without disturbing the aspect ratio of image in Android

View Pager Code

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


    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
       />

</RelativeLayout>

Main Activity

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        android:id="@+id/frameLayout2"

        >

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewPager"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            />

Upvotes: 0

Views: 907

Answers (2)

nshmura
nshmura

Reputation: 6000

centerCrop option may be fit your situation. And adjustViewBounds is not needed.

like this:

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


    <ImageView
            android:id="@+id/image_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"/>

</RelativeLayout>

Upvotes: 1

Saadi
Saadi

Reputation: 1294

Use Pisasso library for Android

Among a lot of other options, it allows you to load and resize images.

An example code could be:

Picasso.with(context)
  .load(url)
  .resize(screeWidth/2, screen height/2)
  .centerCrop()
  .into(imageView);

Upvotes: 1

Related Questions