2017-06-21 64 views
0

centerInsidecenterInside不结垢可绘制到的ImageView

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

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

在我的资产文件夹此一点红方似乎并没有被缩放

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> 

这里我设置绘制的图像视图

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

    } 

} 

任何帮助?谢谢。

回答

0

是的,它做什么它指出:

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

它保持您的图像和大小小于您的父视图。如果你需要的图像以适合您的视图,只需添加这条线在你的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. 
+0

属性您共享似乎并不具备,我想 –

+0

@the_prole我编辑的anwser –

+0

影响的XML看起来像它可能正在工作,唯一的问题是,应用程序栏已经覆盖图像视图 –