Android如何以编程方式调整(缩放)XML矢量图标的大小

问题描述:

这让我发疯.我希望能够以编程方式调整xml矢量可绘制图标的大小,以便在ImageView中使用它.

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 far which is not working

Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_marker,null);
drawable.setBounds(0,0,512,512);
imageVenue.setImageDrawable(drawable);

未调整矢量图标ic_marker的大小.它只是每次都保留硬编码的宽度和高度值.

The vector icon ic_marker is not resized. It just keeps the hardcoded width and height values every time.

有什么想法吗?

您可以通过编程方式更改imageview的宽度和高度.由于矢量可绘制对象将保留图像的原始质量,因此可以实现所需的输出.

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);