Image in ImageView is stretched - Android

让人想犯罪 __ 提交于 2019-12-09 19:47:16

问题


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

<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" />

EDIT If I make the width and height equal to 50dp each, the image gets bigger but is still a flat looking rectangle.


回答1:


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

q1Image.setScaleType(ScaleType.FIT_CENTER);

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




回答2:


Your scaling the Image wrong. Here are all scaletypes:

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

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

You probably need fitCenter:

q1Image.setScaleType(ScaleType.FIT_CENTER);



回答3:


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:

  1. Wouldn't it be better if the layout_weight of the ImageView was zero?

  2. Are you specifying android:stretchColumns on 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_column on the text views.



来源:https://stackoverflow.com/questions/14285007/image-in-imageview-is-stretched-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!