Android how to resize (scale) vector xml icon programmatically

It drives me crazy. I would like to be able to resize a vector vector with the ability of an xml vector programmatically to use it in ImageView.

This is what I have done so far, which does not work

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

The ic_marker vector ic_marker does not change. It just saves stiffness and width values ​​every time.

Any ideas?

+5
source share
2 answers

You can programmatically change the width and height of the image. Since vector drawings will retain the original image quality, this will lead to the desired result.

 ImageView iv = (ImageView) findViewById(R.id.imgview); int width = 60; int height = 60; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height); iv.setLayoutParams(params); 
+2
source

I am currently facing the same problem.

I am trying to do something like this because the ViewParent has the actual height set explicitly, so I use match_parent and set the fields. This does not work all the time, because I just use this view in the viewer for RecyclerView ... I also noticed that sometimes I see an extended version with artifacts, sometimes in full size, sometimes there are fields and more fields ... But it’s all Equally may 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
source

All Articles