Limit image size without cropping

0

How do I limit the size of an ImageButton within a LinearLayout ?

I limited the size of LinearLayout and left the image sizes as wrap_content thinking it would fit the layout size but the image was cropped. When I do not limit the size of the layout the image gets a lot bigger than I would 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>
    
asked by anonymous 21.06.2016 / 14:29

2 answers

0

In order for the image to fit the size of the button, which is determined in this case by the LinearLayout dimensions, use the android:scaleType with the centerInside type. The image will be centered and resized without loss of proportions.

When declaring the TextView dimensions, you should use the sp unit rather than dp , and the height should be declared as wrap_content to prevent the text from being cut in height.

I think it will also be necessary to add the android:layout_weight attribute to the images, otherwise the views will stay 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 button dimensions directly on it and using the android:scaleType="centerInside" attribute to resize the image. In this case the LinarLayout dimensions should / could 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"/>
    
21.06.2016 / 15:18
0

Try to put this line in the declaration of your imageView in XML:

android:adjustViewBounds="true"

This will make the image fit itself into the layout.

    
21.06.2016 / 14:58