Limit image size without cropping

Asked

Viewed 1,630 times

0

How do I limit the size of a Imagebutton within a Linearlayout?

I limited the size of Linearlayout and left the image sizes as wrap_content thinking it would fit the size of the layout but the image was cropped. When I don’t limit the size of layout the picture is much bigger than I’d like it to be.

Code:

  <LinearLayout
    android:layout_width="50dp"
    android:layout_height="70dp"
    android:orientation="vertical"
    android:layout_alignParentEnd="true">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addproduto"
        android:background="@null"
        android:src="@drawable/setacima" />
    <TextView
        android:layout_width="25dp"
        android:layout_height="20dp"
        android:text="0"
        android:textAlignment="center"
        android:textColor="@color/primary_text"
        android:id="@+id/textView" />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:id="@+id/tiraproduto"
        android:src="@drawable/setabaixo"/>

</LinearLayout>

2 answers

0

Try to put this line in your imageView statement in XML:

android:adjustViewBounds="true"

This will cause the image to auto fit in the layout.

0


So that the image fits the size of the button, which is determined in this case by the dimensions of the Linearlayout, use the attribute android:scaleType with the guy centerInside.
The image will be centered and resized without loss of proportions.

In the declaration of the dimensions of Textview should use the unit sp and not dp, in addition that the height should be declared as wrap_content to prevent text from being cut in height.

I believe that it will also be necessary to add the attribute android:layout_weight the images, if not the views will be on top of each other.

<LinearLayout
    android:layout_width="50dp"
    android:layout_height="70dp"
    android:orientation="vertical"
    android:layout_alignParentEnd="true">
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/addproduto"
        android:background="@null"
        android:src="@drawable/setacima" 
        android:scaleType="centerInside"/>
    <TextView
        android:layout_width="25sp"
        android:layout_height="wrap_content"
        android:text="0"
        android:textAlignment="center"
        android:textColor="@color/primary_text"
        android:id="@+id/textView" />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@null"
        android:id="@+id/tiraproduto"
        android:src="@drawable/setabaixo"
        android:scaleType="centerInside"/>

</LinearLayout>

Of course you can also limit the dimensions of the button directly on it and using the attribute android:scaleType="centerInside" to resize the image.
In this case the dimensions of the Linarlayout should/may be declared with wrap_content

<ImageButton
    android:layout_width="100dp"
    android:layout_height="200dp"
    android:id="@+id/addproduto"
    android:background="@null"
    android:src="@drawable/setacima" 
    android:scaleType="centerInside"/>
  • worked out, thank you very much!

Browser other questions tagged

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