java ImageView 中的图像被拉伸 - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14285007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:45:40  来源:igfitidea点击:

Image in ImageView is stretched - Android

javaandroidimageimageview

提问by Matt

I am trying to make a small image appear on my screen. I want it to be a small square. With the below code, it shows up as a long flat rectangle. I have attached a picture of what it looks like below.

我试图在我的屏幕上显示一个小图像。我希望它是一个小正方形。使用下面的代码,它显示为一个长而扁平的矩形。我附上了一张它在下面的样子的照片。

java code

代码

ImageView q1Image = (ImageView)findViewById(R.id.q1Image);
q1Image.setScaleType(ScaleType.FIT_XY);

xml code

xml代码

<TableRow
    android:id="@+id/row4"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <ImageView
        android:id="@+id/q1Image"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_weight=".1"
        android:textSize="8sp"
        android:gravity="center_horizontal" />

test

测试

EDITIf I make the width and height equal to 50dpeach, the image gets bigger but is still a flat looking rectangle.

编辑如果我使宽度和高度相等50dp,图像会变大但仍然是一个扁平的矩形。

回答by Ahmad

Your scaling the Image wrong. Here are all scaletypes:

您缩放图像错误。以下是所有比例类型:

enter image description here

enter image description here

Top row (l-r) center, centerCrop, centerInside.

顶行 (lr) center、centerCrop、centerInside。

Bottom row (l-r): fitCenter, fitStart, fitEnd, fitXY.

底行 (lr):fitCenter、fitStart、fitEnd、fitXY。

You probably need fitCenter:

你可能需要fitCenter

q1Image.setScaleType(ScaleType.FIT_CENTER);

回答by Yaroslav Mytkalyk

FIT_XY will stretch your image to fit all yout ImageView. Use scale type fitCenter to preserve aspect ratio by placing the image in center if your ImageView

FIT_XY 将拉伸您的图像以适合您的所有 ImageView。如果您的 ImageView 将图像放置在中心,请使用比例类型 fitCenter

q1Image.setScaleType(ScaleType.FIT_CENTER);

Refer to http://developer.android.com/reference/android/widget/ImageView.ScaleType.htmlFor other options.

有关其他选项,请参阅http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

回答by Dan Dyer

Without the full layout XML this is just speculation, but seeing as you don't appear to have fixed the problem by changing the scale type, here are some other suggestions:

如果没有完整的布局 XML,这只是推测,但由于您似乎没有通过更改比例类型来解决问题,这里有一些其他建议:

  1. Wouldn't it be better if the layout_weightof the ImageViewwas zero?

  2. Are you specifying android:stretchColumnson the TableLayout?

  3. The image looks like it is being stretched to match the width of the text below. How many columns have you got in your table? It looks like the text in the other rows is in column 0 (as is the image in your first row) and the text in the first row is in column 1. If you want to leave column zero blank for the other rows, you need to specify android:layout_columnon the text views.

  1. 那岂不是更好,如果layout_weightImageView是零?

  2. android:stretchColumnsTableLayout?

  3. 图像看起来像是被拉伸以匹配下面文本的宽度。你的表中有多少列?看起来其他行中的文本在第 0 列中(与第一行中的图像一样),而第一行中的文本在第 1 列中。如果要将其他行的第 0 列留空,则需要android:layout_column在文本视图上指定。