Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?

前端 未结 17 1051
无人共我
无人共我 2020-11-22 17:07

How to fit an image of random size to an ImageView?
When:

  • Initially ImageView dimensions are 250dp * 250dp
  • The image\'
17条回答
  •  情歌与酒
    2020-11-22 17:35

    I am using a very simple solution. Here my code:

    imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.getLayoutParams().height = imageView.getLayoutParams().width;
    imageView.setMinimumHeight(imageView.getLayoutParams().width);
    

    My pictures are added dynamically in a gridview. When you make these settings to the imageview, the picture can be automatically displayed in 1:1 ratio.

提交回复
热议问题