5

Bear with me, I'm new!
I want to use vectors in my android app, and I want my app to be backwards compatible. I found this support library that looks pretty cool!*
So I'm confused about how I would I 'install' it. It gives you a link to download the .pom, .aar, javadoc.jar, and the sources.jar file. Which one should I download, and where (what folder) should I put the file?
(I'm using Android Studio!)
*(Anybody know a different VectorDrawable support library? I'd be interested in hearing everybody's experience!)

Avery Miller
  • 75
  • 1
  • 5

4 Answers4

7

Here is a option that worked for me Use this library - https://github.com/wnafee/vector-compat (api 14+)

android {
    // use version 22 or higher
    buildToolsVersion "22.0.1"
    ...
}
dependencies {
    compile 'com.wnafee:vector-compat:1.0.5'
    ...
}

And create a custom ImageView class that uses vector compat class -

public class SvgImageView extends ImageView {        
    private Drawable icon;


    public SvgImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.button_left, 0, 0);

        try {
            int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);            
            if (resId != -1) {
                icon = ResourcesCompat.getDrawable(this.getContext(), resId);

            }


        } finally {
            ta.recycle();
        }


        if (icon != null) {
            setImage(icon);
        }

    }   

    public void setImage(Drawable icon) {
        SvgImageView.this.setImageDrawable(icon);

    }



}

Vector image example -

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:width="@dimen/logo_dimen"
    android:height="@dimen/logo_dimen"
    android:viewportWidth="@integer/view_port_dimen_logo"
    android:viewportHeight="@integer/view_port_dimen_logo"

    app:vc_viewportWidth="@integer/view_port_dimen_logo"
    app:vc_viewportHeight="@integer/view_port_dimen_logo">
    <group
        android:name="rotationGroup"
        android:pivotX="0"
        android:pivotY="0"
        android:rotation="0">

        <path
            android:name="v"
            android:fillColor="@color/white"
            android:pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.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.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z" />

    </group>
</vector>

Example -

<packagename.SvgImageView     

            app:b_icon="@drawable/google_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView3" />
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
  • Hey @attels! Thanks for the quick response! Just to make sure, all you do is add the android { // use version 22 or higher buildToolsVersion "22.0.1" ... } dependencies { compile 'com.wnafee:vector-compat:1.0.5' ... } – code and add the custom class, and that's it? – Avery Miller Jun 08 '15 at 21:11
  • Add the dependencies in gradle file, add the class, add vector image in drawable folder, and use SVGImageView instead of ImageView, in packagename set your package where you added the SVGImageView. logo_dimen = 20dp, view_port_dimen_logo = 25 – Mikelis Kaneps Jun 09 '15 at 08:58
  • Hi, can I use this library in my Eclipse project. I can't use Android Studio at the moment. I am using Eclipse. – Solace Jul 20 '16 at 09:17
  • 1
    @Solace it is possible to use Gradle in Eclipse, with tools like https://gradle.org/eclipse/, but you can always just copy paste the code from the lib – Mikelis Kaneps Jul 20 '16 at 09:19
  • I have been trying to use Gradle in Eclipse using the Buildship plugin, but when I try to import the AS project of this library, it fails with the error `Synchronize Gradle builds with Workspace failed due to an error in the referenced Gradle build...`. I want to copy the project in Eclipse, but I am not sure about the dependencies declared in its build.gradle. I posted a question [here](http://stackoverflow.com/questions/38477849/which-third-party-dependencies-are-declared-in-the-build-gradle-of-this-android). I'll be very thankful if you can take a look? – Solace Jul 20 '16 at 09:50
  • 1
    @Solace easiest option is to copy all clasess from here https://github.com/wnafee/vector-compat/tree/master/library/src/main/java/com/wnafee/vector and value folder from here https://github.com/wnafee/vector-compat/tree/master/library/src/main/res – Mikelis Kaneps Jul 20 '16 at 10:37
  • @MikelisKaneps I did that. I am getting a strange error at line 84 of [this](https://github.com/wnafee/vector-compat/blob/master/library/src/main/java/com/wnafee/vector/compat/DrawableCompat.java) file: `The return type is incompatible with Drawable.setLayoutDirection(int)` – Solace Jul 20 '16 at 11:41
  • 1
    @Solace yes, I see. Just comment out that method, you don't need it probably. – Mikelis Kaneps Jul 20 '16 at 11:44
  • Now I have got many compiler errors like "Call requires . Current is 14". I think it's time for me to start looking for an alternate to vector drawables for my app. – Solace Jul 20 '16 at 11:54
  • 1
    @Solace Library min sdk is 14, it should work at 14. – Mikelis Kaneps Jul 20 '16 at 11:57
  • @MikelisKaneps Yeah but complains. Actually there should be no errors at all as I copy-pasted all files, but it complains. – Solace Jul 20 '16 at 12:10
  • @MikelisKaneps I think it has to do with dependencies of the library project mentioned in the end of [this build.gradle file](https://github.com/wnafee/vector-compat/blob/master/library/build.gradle). – Solace Jul 20 '16 at 12:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117809/discussion-between-mikelis-kaneps-and-solace). – Mikelis Kaneps Jul 20 '16 at 12:15
4

Google just announced Android Studio 1.4 with backwards compatibility for Vector Drawables. It will generate .png files in the appropriate sizes for the different screen densities for pre-Lollipop devices and will use the vector format for Lollipop and up. See this link: http://android-developers.blogspot.com/2015/09/android-studio-14.html

Just make sure that your Gradle Build version is 1.4.0 or above!

wbonnefond
  • 51
  • 2
  • technically true, but I'm here searching because i got build errors with the built in converter. – mix3d Feb 01 '16 at 03:49
4

Thanks for the people who ported this lib before Google!

Great news is that google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables !

Note:

- Vector images all the way back to API 7 (Android 2.1 Eclair).

- Animated vectors are a bit more limited, going only as far back as API 11

LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
2

The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter.

BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+. The lib can be added to a project with just one line (see readme).

SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. Online version is here.

Links point to readmes, which provide enough information on how to use the lib and the converter.

A-student
  • 785
  • 1
  • 5
  • 12