http://www.jcodecraeer.com//a/anzhuokaifa/androidkaifa/2017/0806/8345.html

Before I explain what Additive Animation is, let me explain why I use it with a simple demonstration.

The user touches and swipes the screen, and the yellow square view is constantly animated as the finger moves.

Note: Based on the animation between the new coordinates and the old coordinates, rather than directly clicking from point to point.

Here’s a normal version using the standard Android animation system:

You can see that the speed is interrupted, and the view doesn’t even move when the finger is across the screen – the animation has restarted before it even runs. The same interaction using additive Animation goes like this:

In our indoor positioning applications, we calculate the user’s current position on a regular basis (similar to Google map that indicates the location you blue dot), when there is a new position is calculated, we use the animation transition to the new points, in Android, it will cancel the current animation began a new animation, resulting in the movement of the incoherent. There’s no problem implementing this on iOS (because every UIView-based animation is superimposed after iOS8).

While Google’s recently released physics-based Animation API can achieve a similar effect, their API requires significant code changes and is clunky and unintuitive to use in most cases.

Instead, let’s take a look at the code in the previous example:

It’s as simple as that, and if it sounds familiar, it’s because I intentionally kept it close to the ViewPropertyAnimator API to make the transition as easy as possible.

Animation supports a variety of properties:

  • All properties supported by ViewPropertyAnimator (X, Y, Z, translationX, translationY, translationZ, Rotation, alpha, etc.)

  • Margins, padding, size, elevation, and the scroll position of the view with the appropriate layout attributes

  • The background color of a view with ColorDrawable as its background

  • Delta values for almost all properties (the *By() method of ViewPropertyAnimator).

  • Move the view along the path

A more complicated example

Here is a slightly more complex animation involving multiple Views, hardware layers, and latency:

Here is the code to create this animation:

There’s a lot going on here, so let’s break it down:

WithLayer () enables hardware animation for all views that are sequentially added to the animation. All views in this example use transparency less than 1 to prove that performance is not affected. Thanks to the highly optimized withLayer() and AdditiveAnimator, hundreds of animations can run at once and feel silky smooth. The above example has between 800 and 1200 animations running at the same time (which kills most apps).

Target (View v) Changes the object on which the animation is applied. AnimatorSet is not required to animate multiple Views.

X (int x), y(int y), rotation(float Degrees) are methods for creating animations, just like the ViewPropertyAnimator equivalents.

The last method, thenWithDelay(int millis), is one of my favorite features of AdditiveAnimator and is worth explaining in its own right.

One of the most common uses of an AnimatorSet is to play animations sequentially (as well as simultaneously).

In general, you need to do this (thanks StackOverflow) :

The same animation created using AdditiveAnimator looks like this:

The then() method creates a new AdditiveAnimator with a parent-child relationship to the previous one, which copies all of its properties (target, duration, etc.), And inside it, set startOffset to the duration of the previous animation (700ms in this case). The new animator can also be configured and modified as needed.

Then () has more features, all of which are much more convenient than standard animation apis:

  • ThenAfterDelay (int millis) waits for Millis milliseconds after the last animation starts to execute the next animation.

  • ThenBeforeEnd (int millis) How long to start before the end of the previous animation.

  • ThenAfterEnd (int millis) How long after the end of the previous animation begins.

Here is a comparison of these methods using the same millis value:

thenAfterDelay(400),

 thenBeforeEnd(400) 

Given that it’s too much to cover in one article, this is just a rundown of the main features.

AdditiveAnimator…

  • Extensions can be implemented through inheritance. Exposes the necessary methods for subclasses, such as those for composition or animation. Suppose you define a bounceTwice() in your MyAnimator:

  • You can also animate custom properties without inheritance. This is done by defining a getter and setter for a property value and providing custom TypeEvaluators. Here is an example of using them to animate the text color of a TextView:

  • Simultaneous animation of multiple views without using AnimatorSet or other encapsulation

  • You can animate along paths – since all animations can be superimposed, you can animate along several paths at once to create one of these animations.

  • Support syntactically elegant chain animation (then(), thenAfterDelay(int millis), etc.).

  • Allows switching of timeinterpolators halfway, which is useful, for example, when you want to animate a spring to a position without letting the bounce affect the transparency value.

  • automatically handles shortest-distance rotation computation for you (never worry about computing the shortestangle again).

  • Hardware layer animation is supported, using the familiar withLayer() syntax.

  • Includes intuitive standard apis, such as animation cancellation (all or specific animations), adding multiple Start /end/ Update listeners, setting repetition mode/times, and getting the current animation object (or the animation object in the queue that hasn’t started yet).

  • If no value is specified, a global default value (such as TimeInterpolator or animation Duration) will be used.

It would take a long time to cover these features in a single article, so see the demo source for details.

Click on the blank area below to see the answer

“I can’t be seen on my phone”


Other styles can be inserted at a fixed height of 180px, and the height can be adjusted by modifying the code!

The library supports API Level 14+ (Android 4.0+). To reference this library in your project, add it to the build.gradle file:

Click on the blank area below to see the answer

“I can’t be seen on my phone”


Other styles can be inserted at a fixed height of 180px, and the height can be adjusted by modifying the code!

  1. Does freelancing make sense for programmers?

  2. Introduction to physics-based animation in Android

  3. Technical interview nine taboo

Every day for you to push Android dry goods and the latest, freshest, most interesting Internet technology information!