How to scale an Image in ImageView to keep the aspect ratio

前端 未结 25 2234
孤独总比滥情好
孤独总比滥情好 2020-11-22 04:59

In Android, I defined an ImageView\'s layout_width to be fill_parent (which takes up the full width of the phone).

If the imag

25条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 05:09

    This is how it worked for me inside a ConstraintLayout:

    
    

    Then in code, I set the drawable as:

    ImageView imgView = findViewById(R.id.myImg);
    imgView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.image_to_show, null));
    

    This fits the image nicely according to its aspect ratio and keeps it in centre.

提交回复
热议问题