Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

Any ideas?

like image 534
ThanosFisherman Avatar asked Jun 28 '16 02:06

ThanosFisherman


People also ask

How to resize vector drawable in Android?

VectorDrawable jDrawable = (VectorDrawable) getContext(). getDrawable(nResourceID); // resize the icon jDrawable. setBounds(30, 30, 30, 30); // set the icon in the button view button. setCompoundDrawablesRelativeWithIntrinsicBounds(jDrawable, null, null, null);

What is not an advantage of using vector Drawables?

The drawback is that they might take longer to draw, etc since there's more of parsing and drawing going on than just using a bitmap. This should although be neglectable if you keep your vectors simple.

Is SVG compatible with Android?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.

What is a vector in XML?

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.


1 Answers

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);
like image 151
Farhad Faghihi Avatar answered Sep 22 '22 21:09

Farhad Faghihi