In Android, you can use AppCompatImageView class to display an image file. Image file is easy to use but hard to master, because of the various screen and dpi in Android devices. As AppCompatImageView is advance version of ImageView so we will use AppCompatImageView, and it's highly recommend to use AppCompatImageView.

public class AppCompatImageView extends ImageView implements TintableBackgroundView
↳ android.view.View
  ↳ android.widget.ImageView
We Recommend to use new UI components i.e. androidx components

A ImageView which supports compatible features on older versions of the platform, including:

  • Allows dynamic tint of its background via the background tint methods in ViewCompat.
  • Allows setting of the background tint using backgroundTint and backgroundTintMode.
  • Allows dynamic tint of its image via the image tint methods in ImageViewCompat.
  • Allows setting of the image tint using tint and tintMode.

ImageView ScaleType

value Description
center Displays the image centered in the view with no scaling.
centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; centers the image in the view
centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center
fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view
fitStart Same as fitCenter but aligned to the top left of the view
fitEnd Same as fitCenter but aligned to the bottom right of the view
fitXY Scales the x and y dimensions to exactly match the view size. dDoes not maintain the image aspect ratio
matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image

Let's create AppCompatImageView in xml

    android:scaleType="fitCenter" />

We can also create AppCompatImageView programatically as below

AppCompatImageView imgView = new AppCompatImageView(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

There is a powerful system for selecting the correct image asset for the correct device. There are specific drawable folders for each device density category including: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high) etc. For example, a drawable-mdpi stands for drawable medium dots per inch.