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

后端 未结 2 667
情书的邮戳
情书的邮戳 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 03:49

    You can change the width and height of your imageview programmatically. Since vector drawables will preserve the original quality of the image, this will make the desired output happen.

    ImageView iv = (ImageView) findViewById(R.id.imgview);
    int width = 60;
    int height = 60;
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
    iv.setLayoutParams(params);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题