centerInside不会将Drawable缩放到ImageView

时间:2017-06-21 03:13:47

标签: android

centerInside

的说明
  

均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或小于视图的相应尺寸(减去填充)。

我想这个描述意味着图像应该在它的视图容器中缩放到最接近它的边缘。

我的资源文件夹中的这个小红点似乎没有缩放

enter image description here

这是我的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>

在这里,我将drawable设置为图像视图

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) { 

    }

}

有任何帮助吗?感谢。

1 个答案:

答案 0 :(得分:1)

是的,它做了它所说的:

  

均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或小于视图的相应尺寸(减去填充)。

它维护您的图像和大小小于父视图。如果您需要图像以适合您的视图,只需在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.