2012-07-26 69 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