7

I want to make an image fit into my image view. I am loading the image programmatically using Glide. It does load the image but does not stretch it to fit my entire image view. It leaves blank spaces on both sides like this

I want to display my entire image without cropping so I can't use

android:scaleType="centerCrop"

There's a way to fit the entire image into my image view by using

android:background="@drawable/image"

It stretches my image to fit the entire image view. But how can I achieve the same if i am loading the image using Glide ?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ashutosh Patoa
  • 294
  • 4
  • 20

4 Answers4

14

I finally got the answer to it. Simply use

android:scaleType="fitXY"

in the xml ImageView tag like this and load image using Glide or BitmapFactory without using any scaleType attribute there.

<ImageView
 android:id="@+id/image_view"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:scaleType="fitXY"/>

Load Image to the image View

Glide.with(this).load(imageUrl).into((ImageView)findViewById(R.id.image_view));
Ashutosh Patoa
  • 294
  • 4
  • 20
2

You can use centerCrop method.

Glide.with(context).load(url).centerCrop().placeholder(R.drawable.default_image).into(img);     
2

If you want your images to fit width and have a dynamic height, without cropping the content, for ex: Like instagram feed, set the height to wrap content, width to match parent and set adjustViewBounds to true.

  <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="200dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
Ajith M A
  • 3,838
  • 3
  • 32
  • 55
0

Try this, which show your full image in center.

imageview.setScaleType(ScaleType.FIT_CENTER);
Janvi Vyas
  • 732
  • 5
  • 16