简体   繁体   English

VectorDrawable向后兼容性和安装非官方支持库

[英]VectorDrawable Backwards Compatibility And Installing Unofficial Support Libraries

Bear with me, I'm new! 忍受我,我是新人!
I want to use vectors in my android app, and I want my app to be backwards compatible. 我想在我的Android应用程序中使用向量,我希望我的应用程序向后兼容。 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. 它为您提供了下载.pom,.aar,javadoc.jar和sources.jar文件的链接。 Which one should I download, and where (what folder) should I put the file? 我应该下载哪一个,以及我应该把文件放在哪里(什么文件夹)?
(I'm using Android Studio!) (我正在使用Android Studio!)
*(Anybody know a different VectorDrawable support library? I'd be interested in hearing everybody's experience!) *(有人知道不同的VectorDrawable支持库吗?我有兴趣听听每个人的经历!)

Here is a option that worked for me Use this library - https://github.com/wnafee/vector-compat (api 14+) 这是一个适合我的选项使用此库 - 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 - 并创建一个使用vector compat类的自定义ImageView类 -

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.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.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"
            app:vc_fillColor="@color/white"
            app:vc_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.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.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" />

Google just announced Android Studio 1.4 with backwards compatibility for Vector Drawables. 谷歌刚刚发布了Android Studio 1.4,向后兼容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. 它将生成适当大小的.png文件,用于前Lollipop设备的不同屏幕密度,并将使用Lollipop及其向量格式。 See this link: http://android-developers.blogspot.com/2015/09/android-studio-14.html 请参阅此链接: 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! 只需确保您的Gradle Build版本为1.4.0或更高版本!

Thanks for the people who ported this lib before Google! 感谢在Google之前移植此lib的人们!

Great news is that google released Android Support Library 23.2 Support Vector Drawables and Animated Vector Drawables ! 好消息是谷歌发布了Android支持库23.2支持向量Drawables和动画矢量Drawables!

Note: 注意:

- Vector images all the way back to API 7 (Android 2.1 Eclair). - 矢量图像一直回到API 7(Android 2.1 Eclair)。

- Animated vectors are a bit more limited, going only as far back as API 11 - 动画矢量有点受限,只能追溯到API 11

The best solution I found is the BetterVectorDrawable lib together with the SVG to VectorDrawable Converter . 我找到的最佳解决方案是BetterVectorDrawable lib以及SVG到VectorDrawable Converter

BetterVectorDrawable is the VectorDrawable implementation for Android 4.0+ with configurable fall-back behavior on Android 5.0+. BetterVectorDrawable是Android 4.0+的VectorDrawable实现,在Android 5.0+上具有可配置的后退行为。 The lib can be added to a project with just one line (see readme). 可以将lib添加到只有一行的项目中(参见自述文件)。

SVG to VectorDrawable Converter is the batch converter of SVG images to Android VectorDrawable XML resource files. SVG to VectorDrawable Converter是将SVG图像批量转换为Android VectorDrawable XML资源文件。 Online version is here . 在线版本在这里

Links point to readmes, which provide enough information on how to use the lib and the converter. 链接指向自述文件,它提供有关如何使用lib和转换器的足够信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM