shaithana
shaithana

Reputation: 2490

Set margins of ImageView whit scaleType="fitCenter"

I have an activity in portrait mode. Inside the activity I want to visualize an image (not matter dimensions and proportions) that automatically fit the exactly width of screen (imagine a normal gallery app). To do this, I place the ImageView in a RelativeLayout, in this way:

<RelativeLayout
    android:id="@+id/relLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp">

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

</RelativeLayout>

and it's ok, BUT now I need to move the ImageView a certain amount of DP down (ex 300dp) so that the bottom of ImageView comes out of the screen (this is because later I will start an animation that will bring the ImageView in the correct position).

If I set layout_marginTop="300dp" for RelativeLayout the ImageView will be scaled down, and is boundaries stop matching the parent width.

I know that I can set the scaleType of ImageView to center, but this way the ImageView stop scaling correctly to adeguate his dimensions to the screen.

The only solution that I found is to wrap RelativeLayout inside a ScrollView, and move the marginTop of the same ScrollView.

There is another way? Hope I explained my problem, thanks.

Upvotes: 0

Views: 521

Answers (3)

vokilam
vokilam

Reputation: 10313

What if you also set layout_marginBottom="-300dp" and animate both properties?

Upvotes: 1

Nitesh Garg
Nitesh Garg

Reputation: 411

Please try setting your layout_marginBottom as -300dp.

This will push your layout out of the screen from bottom without altering the view's height.

Upvotes: 1

Jim
Jim

Reputation: 10278

Don't use "scaleType" - best thing to do is use java (unless you are required to do this with XML - which may mean something more creative, if it's even possible).

Check this out:

Android: How to stretch an image to the screen width while maintaining aspect ratio?

Upvotes: 1

Related Questions