如何禁用ImageView中图像的拉伸?

3

这个视图中有一些ImageView和与之对应的图像。ImageView的大小为100px/100px,但是图像的大小只有50px/50px。我不希望图像填满整个ImageView,也就是说,我希望ImageView在不拉伸图像的情况下将图像居中显示。我该怎么做呢?

更新: 对不起,我犯了一个错误。我的ImageView的大小是50px/50px,而我的图像的大小是100px/100px。你认为这该怎么办呢?

4个回答

3

将以下代码添加到您的 ImageView 中:

android:scaleType="center"

请注意,在Android Studio的xml布局预览中,如果您使用android:background="@drawable/my_image",您仍将看到您的图片被拉伸(在运行时不会被拉伸)。但是,如果您仍想在IDE预览中看到您的ImageView背景(没有拉伸),请改用android:src="@drawable/my_image"而不是background属性。(在任何情况下,scaleType应该是center)

1
你需要在图像视图上设置android:scaleType属性。如果图像比你的视图大,可以使用centerInside或其中一个fitXYfitStartfitEnd选项。你可以在Android开发者文档中获得ImageView类的所有信息。

请看我的更新问题。它的意思是:现在我的图片比我的ImageView大,而ImageView被拉伸了。 - user1841247

0
在 ImageView 中,我们有一个名为 scaleType 的属性,如下所示的代码。
 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="94dp"

    android:scaleType="center"

    android:src="@drawable/ic_launcher" />

它不会缩放您的图像。


0

设置android:scaleType的适当值,您可以使用centerCrop

例如:android:scaleType="centerCrop"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接