2017-06-21 45 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看起來像它可能正在工作,唯一的問題是,應用程序欄已經覆蓋圖像視圖 –