Android how to resize (scale) an xml vector icon programmatically

后端 未结 2 678
情书的邮戳
情书的邮戳 2021-02-06 03:42

This is driving me crazy. I would like to be able to resize an xml vector drawable icon programmatically in order to use it in an ImageView.

This is what I\'ve done so f

2条回答
  •  有刺的猬
    2021-02-06 04:06

    I'm currently facing the same problem.

    I'm trying something like this, cause ViewParent has actually height set explicitly, so I use match_parent and set margins. It doesn't work all the time though, cause I simply use this view in a viewholder for RecyclerView... Also I've noticed that sometimes I see scaled up version with artifacts, sometimes full size, sometimes there are margins, and bigger margins... But it still might work for you, if you use it in a simpler scenario.

    mImageViewFront.setImageDrawable(vectorDrawable);
    final int paddingLR = mImageViewFront.getWidth() / 4;
    final int paddingTB = mImageViewFront.getHeight() / 4;
    LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    params.setMargins(paddingLR, paddingTB, paddingLR, paddingTB);
    mImageViewFront.setLayoutParams(params);
    

提交回复
热议问题