Android Development travel: vectordrawable vector map compatibility problem solution

Source: Internet
Author: User

Since the launch of Android 5.0Lollipop, vectordrawable as a vectorization graphic in an Android environment has been rarely used due to compatibility issues, because it can only be used for more than 5.0 of the system, resulting in now how many Android machine can not be used and has been indefinitely put on hold by developers. Before the official solution for compatibility, there are several solutions in the developer community. Like what:

Https://github.com/trello/victor
Https://github.com/telly/MrVector
Https://github.com/wnafee/vector-compat

However, in terms of effectiveness and ease of use, I personally do not look good, where mrvector has stopped maintenance, and Vector-compat has not been updated for a long time, and the Bo Master test found only for Android Gradle plugin version of 1.4 or less, There is an issue that cannot be compiled correctly after 1.5.

Officials have also not given a VECTORDRAWABLECOMPAT support library to address compatibility issues.

Android Gradle Plugin 1.5 was released and added a new feature related to Vectordrawable. Android build Tools provides another solution to compatibility, and if the compiled version is a version prior to 5.0, build tools will generate the corresponding PNG image for vectordrawable. This allows the generated PNG graph to be used in versions below 5.0, while the vectordrawable is used in more than 5.0 versions

In Build.gradle add generateddensities configuration, you can configure the density of the generated PNG images:

    defaultConfig {        "com.liaoinstan.vectordrawablesupport"        15        23        1        "1.0"        generatedDensities = [‘hdpi‘‘xhdpi‘]    }

Not configured, all generated by default.

So, next test:

Create a project that supports 5.0 of the following versions, I am here to support a minimum of 15, do not configure generateddensities default all Build

    defaultConfig {        "com.liaoinstan.vectordrawablesupport"        15        23        1        "1.0"    }

Under Drawable, create a vector.xml to configure a vector diagram:

<?xml version= "1.0" encoding= "Utf-8"?><vector xmlns:android="http://schemas.android.com/apk/res/android"Android : Viewportwidth="$"android:viewportheight="android:width" =" 500px "android:height=" 500px ">                    <group android:name="Android">        <path  android:name= "head"  android:fillcolor  =" #9FBF3B " android:pathdata  =" M301.3 14,83.298l20.159-29.272c1.197-1.74,0.899-4.024-0.666-5.104c-1.563-1.074-3.805-0.543-4.993,1.199l294.863, 80.53c-13.807-5.439-29.139-8.47-45.299-8.47c-16.16, 0-31.496,3.028-45.302,8.47l-20.948-30.41c-1.201-1.74-3.439-2.273-5.003-1.199c-1.564, 1.077-1.861,3.362-0.664,5.104l20.166,29.272c-32.063,14.916-54.548,43.26-57.413,76.34h218.316c355.861, 126.557,333.375,98.214,301.314,83.298 "/>         <path  android:name= "Left_eye"  android:fillcolor  = "#FFFFFF"  android:pathdata  = "M2 03.956,129.438c-6.673,0-12.08-5.407-12.08-12.079c0-6.671,5.404-12.08,12.08-12.08c6.668, 0,12.073,5.407,12.073,12.08c216.03,124.03,210.624,129.438,203.956,129.438 "/>         <path  android:name= "Right_eye"  android:fillcolor  = "#FFFFFF"  android:pathdata  = "M 295.161,129.438c-6.668,0-12.074-5.407-12.074-12.079c0-6.673,5.406-12.08,12.074-12.08c6.675, 0,12.079,5.409,12.079,12.08c307.24,124.03,301.834,129.438,295.161,129.438 "/>         <path  android:name= "left_arm"  android:fillcolor  = "#9FBF3B"  android:pathdata  = "M1 26.383,297.598c0,13.45-10.904,24.354-24.355,24.354l0,0c-13.45,0-24.354-10.904-24.354-24.354v199.09c0-13.45, 10.904-24.354,24.354-24.354l0,0c13.451,0,24.355,10.904,24.355,24.354v297.598z "/>         <path  android:name= "body"  android:fillcolor  =" #9FBF3B " android:pathdata  =" M140.3 96,175.489V177.915C0,10.566,8.566,19.133,19.135,19.133H22.633V54.744C0, 13.451,10.903,24.354,24.354,24.354C13.451,0,24.355-10.903,24.355-24.354V-54.744H37.371V54.744C0, 13.451,10.902,24.354,24.354,24.354s24.354-10.903,24.354-24.354v-54.744h22.633c10.569, 0,19.137-8.562,19.137-19.133v175.489h140.396z "/>         <pathandroid:name="Right_arm"android:fillcolor="#9FBF3B" android:pathdata="m372.734,297.598c0,13.45,10.903,24.354,24.354,24.354l0,0c13.45, 0,24.354-10.904,24.354-24.354v199.09c0-13.45-10.904-24.354-24.354-24.354l0,0c-13.451, 0-24.354,10.904-24.354,24.354v297.598z " />                                        </Group></vector>

Then add a imageview to the layout to reference the vector diagram:

<ImageView     android:id="@+id/imageView"     android:layout_width="300dp"     android:layout_height="300dp"     android:src="@drawable/vector" />

The effect is as follows:

Then package the compilation, from the project directory to find the compiled generated apk file, the path is as follows:
vectordrawablesupport\app\build\outputs\apk

Then extract the APK using the decompression software, in the Res folder can find the generated individual screen density corresponding resource files:

Different sizes of PNG images are generated for each screen in different resolutions.

This lack of solution solves the current problem of backward compatibility of vectordrawable, but the animatedvectordrawable, which provides vectordrawable with a variety of complex animation effects, still cannot be used in models under 5.0.

At present there is no better solution, generally through the adaptation of the model for different Android version of the production of 2 kinds of resources, one is to support the animation to more than 5.0 of the machine, one is not supported for the animation of the model for 5.0 or less use. When using animatedvectordrawable, animatedvectordrawable resources are placed in the DRAWABLE-V21 directory, and the drawable provides a Animatedvectordrawable the same name as the resource to be used in versions prior to 5.0, in this XML file you can use a selector to replace the animation.

Note that sometimes for brevity it may be possible to put vectordrawable under the Pathdata in String.xml, and then refer to it under vectordrawable, such as:

<path    android:name="right_arm"    android:fillColor="#9FBF3B"    android:pathData="@string/mypath" />

If the generation of PNG to be compatible with 5.0 of the following models, will be reported pathdata error, the compiler will not read string.xml, I do not know why such restrictions, in short, can only write pathdata to vectordrawable.

<path    android:name="head"    android:fillColor="#9FBF3B"    android:pathData="M 12, 4 L 10.59, 13 L 18.99, 13 L 12, 20z" />

Animatedvectordrawable is really a very practical tool, can be configured to achieve a variety of complex animation effects, so that people are bright, but the current compatibility problems are very annoying, another day to write animatedvectordrawable it.

Android Development travel: vectordrawable vector map compatibility problem solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.