bvk256
bvk256

Reputation: 1903

Maximum size of imageviews

I have the layout you see on the picture. Cells for each ImageView are set to be equally sized by width and height. Scale type of images are "fitXY". Currently all images resize depending on screen size and often are upscaled.

I want to achieve the following behavior: set maxSize - maximum width and height of an image. When cells are smaller than maxSize ImageView behave just like with "fitXY", but if cells are bigger than maxSize then images won't upscale further. I don't want to have images for different DPIs, but to resize big icons on the fly.

As I understand there should be different maxSize values for different screen sizes and resolutions.

enter image description here

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:orientation="horizontal"
        android:background="@drawable/app_bg"
        android:weightSum="2"
        android:padding="8dp">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="2">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:src="@drawable/icon_www"
                    android:scaleType="fitXY"
                    android:padding="40dp"/>
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:src="@drawable/icon_fb"
                    android:layout_weight="1"
                    android:padding="40dp"
                    android:scaleType="fitXY"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="2">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:src="@drawable/icon_twitter"
                    android:layout_weight="1"
                    android:scaleType="fitXY"
                    android:padding="40dp"/>

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:src="@drawable/icon_youtube"
                    android:layout_weight="1"
                    android:scaleType="fitXY"
                    android:padding="40dp"/>


        </LinearLayout>
    </LinearLayout>

Upvotes: 1

Views: 5257

Answers (1)

Ankit Aggarwal
Ankit Aggarwal

Reputation: 5375

add android:adjustViewBounds="true" and change 'android:scaleType="center"' to android:scaleType="fitCenter"

<ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:src="@drawable/icon_www"
    android:scaleType="fitCenter"
    android:padding="40dp"
    android:adjustViewBounds="true"/>

EDIT your android:maxHeight isn't working because by giving layout_height as 0dp and layout_weight as 1, you are giving a specific height value to the view. max_height and max_width only works in case your don't have a specific value of height and width for a view. Put wrap_content as the layout_height and remove layout_weight. Similarly by giving layout_width as match_parent, you are defining a specific value for the height of the view. replace match_parent with wrap_content and it will work.

Upvotes: 3

Related Questions