Is it possible to place a bitmap of 100px x 100px in a ImageView of 400dp width and 400dp height? But I don't want to be stretched, I want to center the bitmap in that space. I tried 'scaleType', 'gravity' and it does not work, It keeps increasing its size to fit the ImageView space.
-
Are you using `android:src` to set the image to the `ImageView`? A common mistake is to use `android:background`, which ignores scale type and gravity etc. – Ben P. Aug 03 '21 at 19:13
-
Please just try to create the scaled bitmap with matrix or use just inbound features with that, you can update the bitmap size according to ratio of width height of imageview or also try to use Glide or Picasso library for showing image in imageview – gpuser Aug 03 '21 at 19:23
-
I have to use `android:background`. – Mike087 Aug 03 '21 at 19:44
3 Answers
This works fine:
<ImageView
android:layout_width="400dp"
android:layout_height="400dp"
android:scaleType="centerInside"
android:src="@drawable/fff" />
centerInside:
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view.

- 1,850
- 2
- 11
- 19
You might be able to make it work with padding.
<ImageView
android:layout_width="400dp"
android:layout_height="400dp"
android:padding="300dp"
android:src="@drawable/your_drawable" />
However, you may want to put your imageview inside of a ConstraintLayout. Size the constraintlayout to 400x400dp. Then place the imageview inside, height and width set to wrap_content and center the imageview with constraints.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="400dp"
android:layout_height="400dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_drawable"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The second is more code, but probably the better option if you ever run into different sized pictures. Plus, if the imageview will be clickable, then the second option won't give you a giant invisible button around the "small" imageview.

- 41
- 4
Take a look at another post very similar to your post. ImageView fit without stretching the image
Especially the last part written by Juned Khatri. android:adjustViewBounds="true"

- 38
- 4