2017-02-23 24 views
0

圖像用戶所以我有一個ImageView的,這將檢索數據庫中的圖像,以顯示它的程序。然而,我想要檢索的圖像調整到特定的高度和寬度(因此它不應該填滿整個屏幕)調整輸入到ImageView的

我想它的寬度爲150dp,而它的高度爲230dp。

然而,在數據庫中的圖像可以爲空,正因爲如此,我們不能設置的ImageView的高度和寬度,否則就會出現在佈局上的空白空間。

所以,我怎麼能調整圖片的大小?

以下是我當前的代碼:

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_gravity="center" 
     android:id="@+id/Img" /> 

回答

0

使用規模型fitXY

<ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:scaleType="fitXY" 
      android:layout_gravity="center" 
      android:id="@+id/Img" /> 
0

首先在dimens.xml文件中創建值:

if(image != null){ 
    imageview.getLayoutParams().height = (int) getResources().getDimension(R.dimen.imageview_height); 
    imageview.getLayoutParams().width = (int) getResources().getDimension(R.dimen.imageview_width); 
    imageview.setimagedrawable(img); // or any way that suits you. 
    } 

這樣你就可以翻譯:

<dimen name="imageview_width">150dp</dimen> 
<dimen name="imageview_height">230dp</dimen> 

然後,在你的代碼,你處理從數據庫中收到的圖像dp值與像素值進行比較,並且可以在從xml充氣後以編程方式更改imageview大小。

0

我不是一定要了解你的需求。但是如果你想要一個視圖調整到150x230你需要使用rajahsekar的答案。但是,如果你也希望你的Imaview拿不出地方,如果圖像是空的DB可以設置編程:

imageView.setVisibility(View.GONE)