Android uses vectors (2) To draw graceful path animations _android

Source: Internet
Author: User

With the development of Internet technology, the era of vector images of Android has come. In Google's newest support library v23.2, the AppCompat class has already used vector images, which reduces the AAR package by 9%, about 70KB, to all the newer applications. Of course, we can also use vector, slimming application. Vector images are the manifestation of the SVG format in Android. SVG image adaptation screen, the picture is small, there are many advantages, reference.

The analysis of vectors is mainly divided into two sections:

(1) Use of SVG image slimming applications, reference.

(2) Draw the graceful path animation, the reference.

This article is the second section, about vector animation.

SDK Manager prompts support for library updates

There are three main parts to use vector animations: vector images, path animations, animated-vector images.

The source of this article GitHub download address.

Animation

1. Vector images

SVG-formatted images, converted to vector image resources, can be used with AS2.0 conversion tools or online conversion tools for reference. The image requires a path style that is easy to draw, such as

<vector xmlns:android= "http://schemas.android.com/apk/res/android"
android:width= "256DP"
android: height= "256DP"
android:viewportheight= "android:viewportwidth="
>
<path
android: Name= "Heart1"
android:pathdata= "
android:strokecolor=" #E91E63 "
android:strokewidth=" 1 "/>
<path
android:name= "Heart2"
android:pathdata= "..."
android:strokecolor= "#E91E63"
Android:strokewidth= "1"/>
</vector>

2. Path animation

Use property animation to control the drawing state.

<?xml version= "1.0" encoding= "Utf-8"?> <objectanimator xmlns:android=
"http://" Schemas.android.com/apk/res/android "
android:duration=" 6000 "
android:propertyname=" TrimPathEnd "
android:valuefrom= "0"
android:valueto= "1"
android:valuetype= "Floattype"/>

The Objectanimator Trimpathend property determines the number of path to be drawn, and the remainder is not drawn, with a value range of 0 to 1. The Duration property represents the duration, 6000 or 6 seconds.

3. Animated-vector image

Apply the vector image path (path) to the path animation (objectanimator) to control the drawing.

<animated-vector
xmlns:android= "http://schemas.android.com/apk/res/android"
android:drawable= "@ Drawable/v_heard ">
<target
android:name=" Heart1 "
android:animation=" @animator/heart_animator "/>
<target
android:name=" Heart2 android:animation= "
@animator/heart_animator"
/> ...
</animated-vector>

4. Display animation

Android 5.0 (21) version is required to use vector animation, the Animatedvectordrawable class.

Supports only 5.0 or more.
private void Animateimage () {
if (Build.VERSION.SDK_INT >= build.version_codes. Lollipop) {
//Get animation effect
animatedvectordrawable manimatedvectordrawable = (animatedvectordrawable)
Contextcompat.getdrawable (Getapplication (), r.drawable.v_heard_animation);
Mivimageview.setimagedrawable (manimatedvectordrawable);
if (manimatedvectordrawable!= null) {
manimatedvectordrawable.start ();}}}

The Animatedvectordrawable Start method is the animation startup feature.

Using vector animations to save application resources than GIF animations can give users a better experience. Recommend an interesting SVG library.

The above is a small set to introduce Android using vectors (2) to draw a beautiful path animation, I hope to help you!

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.