很好地缩放Imageview

时间:2012-07-26 10:34:34

标签: android android-layout android-imageview

想法是要么显示照片,要么 - 如果没有定义 - 是一个象征它的小图标。

我在布局中定义了以下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" />

因此,使用'icon'PNG预先初始化ImageView。

加载它的代码:

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 个答案:

答案 0 :(得分:1)

查看ImageView的{​​{3}}标记。对于你的情况,如果你添加

android:scaleType="fitXY"

在布局中的ImageView定义中,图片将调整为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标记在提供的链接中可以采用的其他值。