Why does the image get distorted (doesn’t maintain the proportions) in Imageview?

Asked

Viewed 425 times

5

I have some images inside the folder drawable and call them via code:

 public void inserindoImage(ImageView image,int rid,LinearLayout linear )
    {
        image.setBackgroundResource(rid);
        linear.addView(image);
    }

When calling this function the image goes on the screen.

and stay like this

inserir a descrição da imagem aqui

original image

inserir a descrição da imagem aqui

I would like to know how to make the image keep the proportion in imagemView, as you can see there is a deformation in the image.

xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:id="@+id/scrollView">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearImage"
            android:orientation="horizontal">
            <!--Imagem vem aqui-->
        </LinearLayout>
    </LinearLayout>
</ScrollView>

In case I put only one, but comes more picture so the scroolView.

  • After image.setBackgroundResource(rid); place image.setScaleType(ImageView.ScaleType.CENTER);

  • Are you setting any Layoutparameters for Imageview? Add all relevant code.

  • I just noticed that I made a mistake in scaleType, it is not CENTER but CENTER_INSIDE: image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

  • @ramaral of a look in the edition see if it becomes clearer, I tried but it did not change anything, analyzing the image it fits in the length of the screen but height did not, I needed to move in height.

1 answer

7


Substitute image.setBackgroundResource(rid); for image.setImageResource(rid);

setBackgroundResource() assigns the image to background of view causing her to "stretch" in order to fill it all.

In turn setImageResource() assigns the image to Imageview content, taking into account its dimensions.

On principle, whenever you create a View, should also indicate which Layoutparams to use.

Change the method inserting Image() for:

public void inserindoImage(ImageView image,int rid,LinearLayout linear )
{
    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                      ViewGroup.LayoutParams.WRAP_CONTENT);

    image.setLayoutParams(params);
    image.setImageResource(rid);
    linear.addView(image);
}
  • worked well, but had a space both up and down the image, look at the edition that posted the result

  • This is another problem, ask a new question. Put the xml that produces the result you posted. In xml I don’t see where the background red

  • red comes Relative Layout

  • I asked another question follow the link http://answall.com/questions/119898/como-resolver-espa%C3%A7o-formado-numa-linear-layout-ao-criar-imagew-via-c%C3%B3digo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.