简体   繁体   English

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

[英]How to disable stretching of image in ImageView?

There are some ImageView and image for this view. 该视图有一些ImageView和图像。 ImageView is 100px/100px, but image is 50px/50px. ImageView为100px / 100px,但图像为50px / 50px。 I wouldn't like the image fills full space in ImageView, ie I want that the ImageView contains my image in center of ImageView without stretching. 我不希望图像填充ImageView中的全部空间,即,我希望ImageView在不拉伸的情况下将我的图像包含在ImageView的中心。 How can I do it? 我该怎么做?

UPDATE: Sorry, I've made a mistake. 更新:对不起,我犯了一个错误。 My ImageView is 50px/50px, and my image is 100px/100px. 我的ImageView是50px / 50px,我的图像是100px / 100px。 What do you think about it? 你怎么看待这件事?

Add to your ImageView: 添加到您的ImageView:

android:scaleType="center"

Note that in the Android Studio xml layout preview if you use the android:background="@drawable/my_image" you will still see your image streached (In run time it won't streach). 请注意 ,如果您使用android:background="@drawable/my_image"在Android Studio xml布局预览中,您仍会看到图像变形(在运行时不会变形)。 But if you still want to see your ImageView background (not streached) also in the IDE preview use android:src="@drawable/my_image" instead of background property. 但是,如果您仍然想在IDE预览中查看ImageView背景(未变形),请使用android:src="@drawable/my_image"而不是background属性。 (in any case the scaleType should be center) (在任何情况下,scaleType都应居中)

You need to set the android:scaleType attribute on your image view. 您需要在图像视图上设置android:scaleType属性。 If the image is bigger than your view, either use centerInside or one of the fitXY , fitStart , fitEnd options. 如果图像大于视图,请使用centerInsidefitXYfitStartfitEnd选项之一。

You have all the info you need on the Android developer documentation for the ImageView class. 您在Android开发人员文档中拥有ImageView类所需的所有信息。

set the android:scaleType with proper value, you can give centerCrop . android:scaleType设置为适当的值,您可以给定centerCrop

eg android:scaleType="centerCrop" 例如android:scaleType="centerCrop"

In ImageView we have a property called scaleType that is shown below in the code. 在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" />

It will not scale your image. 它不会缩放您的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM