the_prole
the_prole

Reputation: 8985

centerInside does not scale Drawable to ImageView

The description of centerInside

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

I imagine this description means the image should scale to the edge closest to it in it's view container.

This little red square in my assets folder does not appear to be scaling

enter image description here

Here is my main_activity.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ImageView
        android:id="@+id/redSquare"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerInside"
        android:background="#00ffff"
        />

</android.support.design.widget.CoordinatorLayout>

And here I'm setting the drawable to the image view

ImageView redSqaure;

@Override
protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.main_activity);

    //...

    try {

        InputStream ims = getAssets().open("redSquare.png");
        Drawable d = Drawable.createFromStream(ims, null);

        // Setting the drawable to image view
        redSqaure.setImageDrawable(d);

        ims .close();

    } catch(IOException ex) { 

    }

}

Any help? Thanks.

Upvotes: 1

Views: 1021

Answers (1)

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

yes, it does what it stated:

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

It maintains your image and size is less than your parent view. If you need the image to fit your view, just add this line in your xml:

android:scaleType="fitCenter" // change this flag as well.
android:adjustViewBounds="true" //Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

Upvotes: 4

Related Questions