Thursday, February 11, 2016

Android: How to change size of vector drawable image

Android 5.0 brought support for vector drawables similar to SVG. The lack of backwards compatibility with older versions of Android has probably seen a slow uptake in using it, but the recent addition of vector assets to Android Studio helps to conquer that hurdle illustrates the belief Google have in the merits of wide adoption of vector graphics by Android apps.

It should be noted that the Lollipop support for vector drawables had some kinks which have been ironed out in later Android releases. Most notably in Lolipop scaled vector drawables appear aliased.
from api v23 (Marshmallow) they scale just fine.

So if you do manage to end up in a situation where you want to scale a vector drawable (i.e. your app is only supporting v23+) then the following example (where the icon is by default 24 x 24) is one way to do it.

<ImageView    android:id="@+id/icon"    android:layout_width="240dp"    android:layout_height="240dp"    android:layout_alignParentLeft="true"    android:src="@drawable/my_test_vector"    />

VectorDrawable vectorDrawable = (VectorDrawable) mIconImageView.getDrawable();