0

Im trying to fit images perfectly in the imageView but the image dosent fit properly when i use centerCrop with adjustViewBounds but when i use fitXY with adjustViewBounds the image fits perfectly to the imageView but now the image quality gets worst, My imageView height and width are match_parent and yes i also used fitCenter but didnt worked

Screenshot//currently im using centerCrop with adjustViewBounds

enter image description here

Here is my code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:weightSum="5">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/Card_View"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="5dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="4"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/imagePostHome"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:contentDescription="@string/todo"
                app:shapeAppearanceOverlay="@style/RoundedCornerHome" />

            <include
                android:id="@+id/imagepost_buttons"
                layout="@layout/imagepost_buttons" />
        </FrameLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:background="@color/grey"
        app:cardBackgroundColor="@color/grey"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/material_cardview_snipet"
                layout="@layout/material_cardview_snipet" />
        </FrameLayout>


    </com.google.android.material.card.MaterialCardView>


</LinearLayout>

for those who want to know what is layout material_cardview_snipet ,here is the code of it

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="25dp"
        android:src="@drawable/ic_baseline_attach_money_24"
        tools:ignore="RtlHardcoded"
        android:contentDescription="@string/todo" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Ian Somerhalder"
        android:textColor="@color/white"
        android:layout_marginTop="5dp"
        android:textSize="20sp"
        android:textStyle="bold"
        tools:ignore="SmallSp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        tools:ignore="RtlHardcoded"
        android:textSize="17sp"
        android:layout_marginLeft="10dp"
        android:textColor="@color/white"
        android:text=".............."/>



</FrameLayout>
Vasant Raval
  • 257
  • 1
  • 12
  • 31

3 Answers3

1

You can resize your image in firebase, No need to code for it https://firebase.google.com/products/extensions/storage-resize-images You can use this cool extension of firebase. it Resizes images uploaded to Cloud Storage to a specified size, and optionally keeps or deletes the original image.

it Requires a blaze plan, for that use the trial version of it or if you are a student you can use your .edu id and get it from google.

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

If you're image size I mean height and width ratio don't match with your image view height and width ratio glide will fit the image to center of your image view and a part of the image cuts if the ratios doesn't match. If you force images to fit the image view even the ratios doesn't matches it will be compressed to fit either in x-axis or in y-axis. So your image don't look as original.

So use your image view attributes like this android:layout_width="match_parent" android:layout_height="wrap_content"

Here width is fixed and hight will be calculated according to the image height.

Real Tech Hacks
  • 367
  • 3
  • 11
0

If you are fetching images from a server or even from drawable use the glide library to set the image into an image view. Google also recommends the Glide library to deal with images in android. It's very easy to understand quickly. See Doc

Venkat
  • 384
  • 1
  • 16