如何调整imageView的大小以始终匹配其在android中的图像背景尺寸?

时间:2013-11-10 13:29:01

标签: android image imageview setbackground

我希望imgFirstAct imageView将其宽度精确调整为设置为它的背景的宽度,只要背景比imageView更宽或更薄。

这是imageView xml:

<ImageView
    android:id="@+id/imgFirstAct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imgCopyBelow"
    android:layout_alignLeft="@+id/imgCaloriesBelow"
    android:layout_alignRight="@+id/TextView04"
    android:layout_alignTop="@+id/imgCaloriesBelow"
    android:maxWidth="290dp" />

以下是代码:

private ImageView imgFirstAct;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

          imgFirstAct = (ImageView)findViewById(R.id.imgFirstAct);

          imgFirstAct.setScaleType(ImageView.ScaleType.FIT_START);

          Bitmap bitmap = ((BitmapDrawable)imgCopyBelow.getBackground().getCurrent()).getBitmap ();

          int h = bitmap.getHeight();      

          Bitmap croppedBmp = Bitmap.createBitmap(bitmap, 0, 0, 120, h);    

          Drawable d = new BitmapDrawable(getResources(),croppedBmp);   

          imgFirstAct.setBackground(d);  
}

我在这里设置了一个比imageView更宽的图像,但是imageView并没有扩大到背景图像的宽度。 我该怎么办?

非常感谢:)

2 个答案:

答案 0 :(得分:0)

您应该在Xml布局文件中将填充添加到ImageView,然后填充背景会更宽。

答案 1 :(得分:0)

为了将图像设置为适合imageview的背景,您需要在xml中设置scaletype,如下所示

 android:scaleType="fitXY"