2012-07-26 89 views
0

這個想法是要麼顯示一張照片,要麼 - 如果沒有定義 - 一個小圖標象徵着它。比例圖像很好地

我已經在佈局中定義了以下的ImageView:

 <ImageView 
      android:id="@+id/imgPic" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:contentDescription="@+string/strImgPic" 
      android:paddingRight="10dip" 
      android:paddingTop="10dip" 
      android:saveEnabled="false" 
      android:src="@drawable/pic_icon" /> 

所以ImageView的是預先初始化爲「圖標」 PNG。

的代碼加載它:

try { 
imgURL = vinfo.getString("mainpic_url"); 
    if (imgURL.length() > 1) { 
    imgURL = getString(R.string.urlPicsFull) + imgURL; 
    ImageView imgPic = (ImageView) findViewById(R.id.imgPic); 
    ImageDownloader img = new ImageDownloader(); 
    img.download(imgURL, imgPic); 
    } 
} catch (..) { 
(..) 

ImageDownloader()基本上沒有什麼比異步檢索圖像別的。但問題是,如果沒有圖像(imgURL.length()== 0),圖標將在佈局中的空方塊中間顯示一個非常大的「邊框」。

我希望將圖像縮小(或向上)爲父視圖的寬度,但圖標要顯示爲原樣(大約90度,寬和高)。

有關如何實現這一點的任何建議?

回答

1

查看ImageViewandroid:scaleType標記。對於你的情況,如果你在你的ImageView定義添加

android:scaleType="fitXY" 

佈局,圖像將被調整到你ImageView大小。是這樣的:

<ImageView 
     android:id="@+id/imgPic" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:contentDescription="@+string/strImgPic" 
     android:paddingRight="10dip" 
     android:scaleType="fitXY" 
     android:paddingTop="10dip" 
     android:saveEnabled="false" 
     android:src="@drawable/pic_icon" /> 

然而,這將不保持縱橫比,以便所述圖像可能失真。如果您不想要,請檢查android:scaleType標記在提供的鏈接中可以採用的其他值。

+0

它現在正常工作 - 謝謝! – richey 2012-07-27 05:58:34

+0

很高興我能幫忙:) – Angelo 2012-07-27 06:47:12