<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Ganster on Medium]]></title>
        <description><![CDATA[Stories by David Ganster on Medium]]></description>
        <link>https://medium.com/@david.gansterd?source=rss-cab1034ed86d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*c6pSotzf9fh43mUXXYEDwQ.jpeg</url>
            <title>Stories by David Ganster on Medium</title>
            <link>https://medium.com/@david.gansterd?source=rss-cab1034ed86d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:11:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@david.gansterd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Bringing smooth animation transitions to Android]]></title>
            <link>https://medium.com/wirecube-software-engineering/bringing-smooth-animation-transitions-to-android-88786347e512?source=rss-cab1034ed86d------2</link>
            <guid isPermaLink="false">https://medium.com/p/88786347e512</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[user-experience]]></category>
            <dc:creator><![CDATA[David Ganster]]></dc:creator>
            <pubDate>Mon, 31 Jul 2017 08:04:07 GMT</pubDate>
            <atom:updated>2023-11-23T15:02:02.547Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uLJQGSrCm2WpHy-jhbQUiQ.png" /></figure><h4>Why I wrote the <a href="https://github.com/wirecube/android_additive_animations">AdditiveAnimation Android library</a> and how it drastically improved the feel of our apps without any significant code changes — simultaneously making all animation-related code shorter, more performant and easier to read.</h4><p>Before diving into details of what additive animations are, here’s a simple showcase explaining why you should use them. <br>In this sample app (which I totally stole from <a href="http://ronnqvi.st/multiple-animations">here</a>), the user taps or drags on the screen to trigger an animation that moves the yellow view towards the finger.</p><p>Here’s the normal version using the standard Android animation API:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*bawqTp_FzU-7uw5tcN4yEA.gif" /><figcaption>What changing the animation target usually looks like in Android — ugh!</figcaption></figure><p>Notice the abrupt change in velocity when the target changes. When the user moves their finger across the screen, the view doesn’t move at all — the animation is restarted before it has a chance to run!</p><p>Here’s the same interaction using additive animations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*Zi7Sgn6p73fHPbOI7eBKnQ.gif" /><figcaption>Changing animation target with the AdditiveAnimator library.</figcaption></figure><h3>Additive Animations</h3><p>In our <a href="http://shopmap.io/">indoor location application</a>, we periodically recalculate the current position of the user (think of the blue dot indicating your position in Google Maps). Whenever a new position is calculated, we animate the blue dot to the new position — but in Android, this cancels the currently running animation and starts a new one, resulting in disjointed movement! <br>Having implemented this feature on iOS without any problems (<em>because every </em><em>UIView-based animation is performed additively since iOS 8</em>), I investigated how to fix the discontinuity in momentum and found a concise summary on this topic:</p><p><a href="http://iosoteric.com/additive-animations-animatewithduration-in-ios-8/">Additive animations: animateWithDuration in iOS 8</a></p><p><strong>TL;DR: The currently running animation isn’t removed when adding another animation for the same property</strong> — instead, multiple animations can contribute to the animated value simultaneously. This has minimal impact on performance and can greatly improve the feel of an app — as it did for us in all of the Android apps we develop at <a href="http://shopreme.com/">shopreme</a>.</p><h3>The API</h3><p>While Google has just announced a new <a href="https://developer.android.com/preview/features/physics-based-animation.html">physics-based animation API</a> that can be used to create a <a href="https://developer.android.com/images/guide/topics/graphics/targetchange_pba.gif">similar effect</a>, their new API requires significant code changes and is cumbersome and unintuitive to use in most situations.</p><p>In contrast, let’s look at the code of the previous example:</p><pre><strong>AdditiveAnimator.animate(view)</strong>.setDuration(1000)<br>                .x(touch.getX())<br>                .y(touch.getY())<br>                .start();</pre><p>If this seems familiar, it’s because I deliberately stuck close to the ViewPropertyAnimator API to make conversion as simple as possible.</p><p>Animations are supported out of the box for a lot of attributes:</p><ul><li>Everything that ViewPropertyAnimator supports ( x, y, z, translationX, translationY, translationZ, rotation, alpha etc).</li><li>Margins, padding, size, elevation and scroll position of views with appropriate LayoutProperties.</li><li>Background color of views with a ColorDrawable background.</li><li>Delta values for (almost) everything (the *By() methods of ViewPropertyAnimator).</li><li>Moving a view along a path (optionally, while rotating tangentially).</li></ul><h3>A more complex example</h3><p>Here’s a fairly complex animation involving multiple views and delays between the animations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*_mnqqA56HXp3h93KmgxFAQ.gif" /><figcaption>It looks much better in 60fps (download video <a href="https://github.com/davidganster/android_additive_animations/raw/master/gif/multiple_views.mp4">here</a>).</figcaption></figure><p>Here’s the code it takes to create this animation:</p><pre>AdditiveAnimator.<strong>animate(views, 50L)</strong>.x(x).y(y).rotation(r).start();</pre><p>We simply pass a list of views to the animate method, and an optional stagger parameter (<em>50 </em>milliseconds<em> </em>in this case) which specifies how much of a delay there will be between applying the following animations to the list of views.</p><p>Here’s another, more verbose, way of building this animation that more explicitly shows what is going on under the hood of the multi-view staggered animation:</p><pre>AdditiveAnimator<strong> </strong>anim = new AdditiveAnimator();<br>for (View<strong> </strong>v : views) {<br>    anim = anim.<strong>target</strong>(v).x(x).y(y).rotation(r).<strong>thenWithDelay</strong>(50);<br>}<br>anim.start();</pre><p><strong>target(View v)</strong> changes the current animation target to the given view. There’s no need to use AnimatorSet when animating multiple views!<br>This method is very handy when you want to animate different properties for each view. (There’s also a <strong>targets()</strong> method which takes a list of targets — useful in cases where you want the same animation to be run on multiple views.)</p><p>The final method call in the builder — <strong>thenWithDelay(int millis)</strong> is one of my favourite features of AdditiveAnimator and deserves its own section (see <em>Animation Chaining</em>).</p><h3>Animation Chaining</h3><p>One of the most common use cases of AnimatorSet (besides running multiple animations simultaneously) is playing animations sequentially.</p><p>Essentially, what you would have to do is this (courtesy of <a href="https://stackoverflow.com/a/34750387/1392301">StackOverflow</a>):</p><pre>Animator translationAnimator = ObjectAnimator<br>        .ofFloat(view, View.TRANSLATION_Y, 0f, -100f);<br><br>final Animator alphaAnimator = ObjectAnimator<br>        .ofFloat(view, View.ALPHA, 1f, 0f);<br><br>final AnimatorSet animatorSet = new AnimatorSet();<br>animatorSet.playSequentially(<br>        translationAnimator,<br>        alphaAnimator<br>);<br>animatorSet.start();</pre><p>Here is the same animation built with AdditiveAnimator:</p><pre>AdditiveAnimator.animate(view)<br>                .translationY(-100)<br>                .<strong>then()</strong><br>                .alpha(0f)<br>                .start();</pre><p>The <strong>then()</strong> call in this example creates a new AdditiveAnimator and establishes a parent-child relationship with the previous one, copying all of its properties (such as the animation target, duration etc), and sets its internal startOffset to the total duration of all previous animators (in this case, 700 milliseconds). The new animator can then be configured and modified as needed without affecting the previously created animations. For example, you could set a new duration for each chained animation.</p><p>There are more flavors of then(), all of which are more than inconvenient to create using the standard animation APIs:</p><ul><li>thenAfterDelay(int millis) waits millis milliseconds <strong>after starting the previous animator</strong> before executing the next animation. (“<em>Run 400ms </em><strong><em>after</em></strong><em> the previous animation </em><strong><em>starts</em></strong>”)</li><li>thenBeforeEnd(int millis) lets you specify a <strong>negative</strong> <strong>offset to the end of the previous animation</strong>. (“<em>Run 400ms </em><strong><em>before</em></strong><em> the previous animation </em><strong><em>ends</em></strong>”)</li><li>thenAfterEnd(int millis) lets you specify an offset <strong>after the end of the previous animation</strong>. (“<em>Run 400ms </em><strong><em>after</em></strong><em> the previous animation </em><strong><em>ends</em></strong>”)</li></ul><p>Here’s a visual comparison of all of those functions when called with the same millis value:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/1*wI9MVQ0XVz3F9MHQomkXVw.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*9PooPiifOITecymxcpsWcQ.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*DMXdaVeQkqcSnt8jG_XzlQ.gif" /><figcaption>From left to right: thenAfterDelay(400), thenBeforeEnd(400) and thenAfterEnd(400)</figcaption></figure><h3>Stuff that didn’t make it into the post</h3><p>There is simply too much to talk about for one blog post, so here’s a quick rundown of the most important features.</p><p>AdditiveAnimator…</p><ul><li>was designed to be <strong>easily</strong> <strong>extensible</strong> for your specific use-case by subclassing. There are hooks for subclasses to apply custom property values that don’t easily map to any single property value and all relevant methods for composing and managing animations are exposed (but only the ones that don’t let you shoot yourself in the foot 🔫 😵).<br>Let’s say you defined bounceTwice() in your MyAnimator class. AdditiveAnimator lets you use this method, no matter how you construct your animation:</li></ul><pre>new <strong>MyAnimator</strong>(view).x(100).then().<strong>bounceTwice</strong>().start();</pre><ul><li>can <strong>animate any custom property</strong> additively without subclassing simply by specifying a getter and setter for the property value and provides access to <strong>custom </strong><a href="https://developer.android.com/reference/android/animation/TypeEvaluator.html"><strong>TypeEvaluators</strong></a>. <br>Here’s an example using these two features to animate the text color of a TextView:</li></ul><pre>FloatProperty&lt;TextView&gt; <strong>textColorProperty</strong> = FloatProperty.create(<br>    &quot;textColor&quot;, <br>    tv -&gt; tv.getTextColor(),<br>    (tv, color) -&gt; tv.setTextColor((int) color)<br>);</pre><pre>AdditiveAnimator.animate(myTextView)<br>                .<strong>property</strong>(Color.RED, // target value<br>                          new <strong>ColorEvaluator</strong>(), // TypeEvaluator<br>                          <strong>textColorProperty</strong>)<br>                .start();</pre><ul><li>works with <strong>multiple views</strong> simultaneously without having to use AnimatorSet or any other wrapper.</li><li>can <strong>animate along paths</strong> (including tangential rotation) — and since all animations are additive, you can even animate along multiple paths at the same time, creating an animation such as <a href="http://ronnqvi.st/images/heartLoop.gif">this</a>.</li><li>supports <strong>animation chaining</strong> with elegant syntax (then(), thenAfterDelay() etc.).</li><li>allows you to <strong>switch the </strong><strong>TimeInterpolator midway through</strong> creating an animation. This is particularly useful for when you want a spring animation for the position, but don’t want the bouncing to affect the alpha value:</li></ul><pre>AdditiveAnimator.animate(view)<br>                .<strong>setInterpolator</strong>(new <strong>BounceInterpolator</strong>())<br>                .x(100).y(200)<br>                .<strong>switchInterpolator</strong>(new <strong>LinearInterpolator</strong>())<br>                .alpha(0)<br>                .start();</pre><ul><li>automatically handles <strong>shortest-distance rotation computation</strong> for you (never worry about <a href="https://stackoverflow.com/questions/9505862/shortest-distance-between-two-degree-marks-on-a-circle">computing</a> <a href="https://stackoverflow.com/questions/7570808/how-do-i-calculate-the-difference-of-two-angle-measures">the</a> <a href="https://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles">shortest</a> <a href="http://blog.lexique-du-net.com/index.php?post/Calculate-the-real-difference-between-two-angles-keeping-the-sign">angle</a> again).</li><li>supports <strong>hardware layer animations</strong> using the familiar withLayer() syntax.</li><li>includes <strong>intuitive APIs</strong> for standard features like cancellation of all or just a few specific animations, adding multiple start/end/update listeners, setting a repeat mode/count and getting the current animation target value (or the latest queued value for animations that haven’t been started yet).</li><li>allows for <strong>global setup of default values</strong> to be used in all animations if no other value is specified (like the TimeInterpolator or animation duration).</li><li>is highly profiled and optimized for <strong>performance</strong>. All allocations are made at animation build-time so that there are no GC-related hiccups while the animation is running.</li></ul><p>Going into detail about all of these features is too much for a single blog post, so stay tuned for more code samples, demos and new feature announcements!</p><h3>Getting the library</h3><p>The library supports API Level 14+ (Android 4.0+). The source code, complete with a demo project containing all referenced samples and more, is available on <a href="https://github.com/wirecube/android_additive_animations">GitHub</a>, and you can grab the newest version of the library by adding</p><pre>dependencies {<br>    compile <strong>‘at.wirecube:additive_animations:1.9.3’<br></strong>}</pre><p>to your build.gradle file.</p><p>If you have feature requests or suggestions for improvements, feel free to open a ticket on GitHub or leave a comment!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88786347e512" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wirecube-software-engineering/bringing-smooth-animation-transitions-to-android-88786347e512">Bringing smooth animation transitions to Android</a> was originally published in <a href="https://medium.com/wirecube-software-engineering">wirecube software engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>