florian h
florian h

Reputation: 1171

Stretch ImageView so that it fills the whole screen in a Dialog

I have an app with a lot of small images in it. To provide an "enlarge" function I'd like to open a Dialog (realized as an Activity with dialog theme) containing only an ImageView with the selected picture.

The image/dialog should be displayed as large as possible (i.e. scaled proportionally to the screen size) but also only as large as needed. That means that for a landscape picture the dialog should also be in a landscape format instead of filling the whole screen (with black bars at the top/bottom). Basically I want the dialog to be as large as the ImageView and the ImageView as large as possible (while regarding the aspect ratio of the containing image).

Is there a way to achieve that?

Please note that the displayed image might be a lot smaller than the available screen space but it should be scaled up in this case. I can't provide images for different resolutions/screen sizes as it's user-generated content.

My current layout file for the dialog (which works fine for pictures larger than the device display but won't scale up images that are smaller):

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:background="@color/image_overlay" <!-- semi transparent background -->
             android:gravity="center"
             android:foregroundGravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:scaleType="centerInside"
        android:layout_margin="8dp"/>
</FrameLayout>

Upvotes: 0

Views: 609

Answers (1)

tar
tar

Reputation: 1568

Use ScaleType.FIT_XY. Bear in mind that this will ruin the aspect ratio (because the image will be stretched). The official definition says

Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.

For example in your Java code, use

ImageView image = (ImageView) findViewById(R.id.img_background);
imgview.setScaleType(ScaleType.FIT_XY);

Equivalently, in your XML you may use:

<ImageView
    ...
    android:scaleType="fitXY" />

Upvotes: 2

Related Questions