Search code examples
androidimageviewandroid-imageview

Setting ScaleType.MATRIX to Custom ImageView shrink the initial display of image


I'm just about to create my own custom ImageView, so I make the class below.

class MyImageView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {
}

By default (i.e. without setScaleType(ScaleType.MATRIX)), it looks below (fit well to the screen).

enter image description here

So if I add setScaleType(ScaleType.MATRIX) in the init it became as below.

class MyImageView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {


    init {
        setScaleType(ScaleType.MATRIX)
    }
}

enter image description here

Why was it shrink? What Can I do to still use ScaleType.MATRIX yet have it initially display fitting the screen?


Solution

  • The Matrix scaletype is to apply no scale type to it by default, hence the image will get resized to its original size.

    In this case, the image is smaller than the view, hence it is shrinked smaller. Without specifying the ScaleType, it will FitCenter, hence enlarge the smaller image to fit the view size.

    More explanation here