20
votes

EDIT 2: new official training guide

The Developers site released a training guide for UI related stuff, this is the index:

  • Animations Overview
  • Property Animation Overview
  • Animate drawable graphics
  • Reveal or hide a view using animation
  • Move a View with Animation
  • Move views using a fling animation
  • Enlarge a view using a zoom animation
  • Animate movement using spring physics
  • Auto animate layout updates
  • Animate layout changes using a transition
  • Create a custom transition animation
  • Start an activity using an animation

If you are interested in any of these, this is the link: https://developer.android.com/training/animation/


EDIT: Answers sum up

I found 5 ways to animate in Android:

  1. Animate the properties of a View with Property Animation to smoothly change rotation, alpha, scale etc.

  2. Frame Animations (AnimationDrawable): change the pictures quickly, so that it looks animated

  3. Design the images with vectors (VectorDrawable) and animate them by editing them over time with AnimatedVectorDrawable.

  4. Override onDraw() on a View and perform Custom Drawing by painting in the canvas.

  5. Use Lottie, what reproduces animations from After Effects. Many animations available at LottieFiles.

However, Android provides some built-in tools too, such as Scenes (that let you animate the transition among several layouts that share the Views), Shared elements (that lets you make the ilussion of passing a View from one Activity to another one) etc.

Many (if not all) of these features were added in API 21, click here here for more information.

Here are some interesting articles/blogs on animation:

Last, a couple interesting tools:

  • Mac tool to record Android screen on GitHub.

  • Video to GIF converter online.


Note

I am aware Android provides transformations such as scale, alpha, rotate, translation etc.

Examples

There are 2 examples I would like to look at and compare.

1 - Custom View animations

For example, filling up a glass of water or drawing a path.

2 - Complex View animations

For instance, StackExchange App for Android, login screen animation (couldn't find a video on it, also, didn't check if behaves the same in iOS).

Question

For the first example, I can't think of any other way than playing GIFs, or manually changing images after little time periods.

I do not think this is the case, that's why I would like to ask, (1) do you know how it's done?

Regarding the second example, only one idea came to my mind, and that's setting a Path and moving the View accordingly by passing it somehow after animate(). (2) Is this possible?

Apart from the mentioned above, (3) do you know of other techniques to play animations? (Such as Scene transitions - mentioned in an answer-)

Please share! Thank you.

4
Have you considered animated vector drawables?azizbekian
@azizbekian Didn't know about it, but looks super tedious to work with. Looking at the example at Vector Drawable guides webpage. I think it's meant for simpler images. Do you think you can achieve the same effect as the images I provided ?JonZarate
Theoretically you can. But vector drawables are mainly for icons. If the path in svg is rather simple, that it won't take too much time to render it. Here you can see more details how it can be implemented.azizbekian
@azizbekian If you provide an example that is similar to the water glass and answer the question on how to get an animation like the SO App login I would mark it as the best answer.JonZarate
Can't understand what animation is at SO App login. On my phone it's the latest version, and it is only splash screen with progress bar.azizbekian

4 Answers

10
votes

"Filling up a glass of water" animation is direct canditate of implementing via frame animation, i.e. changing pictures one after the other. Here you can see a nice blog post describing how to implement this kind of animation, which basically is the same as "filling up a glass of water" you mentioned:

enter image description here

The other animation look slightly difficult at first glance.

enter image description here

But after turning on "Show layout bounds" you can see there is no magic there. Basically this is just a translation animation, which translates a view from one position to another. In case of this animation the difficult part is to implement the algorithm of finding translation coordinates. After that animating is just a couple lines of code via scene transition animation.

// assuming at this step all the views are at the initial position at x0, y0
TransitionManager.beginDelayedTransition(rootLayout);
// here set view coordinates to x1, y1 - the final position

Transitions framework will do the rest for you. It will find the delta and perform animation for you. Here you will find a nice article by Lucas Rocha.

6
votes

do you know of other techniques to play animations?

In Android you have basically 3 ways to implement animations:

  • Animate the View (Scale, alpha, rotate, etc) by just using view.startAnimation(...)
  • Animate the Drawable (Depending on the Drawable, this could be anything from animated vector drawables to frame animations)
  • Doing custom drawing and animation

Obviously just animating a view by framework methods is easier than creating some drawable animation, and creating some drawable (xml) is easier than doing custom drawing.

You mentioned lottie, which just came out a few days ago. We shall see how well it does, but it looks very promising. Under the hood lottie will parse the json and do some custom drawing using Canvas and Path (3rd bullet point above)
If you can get your hands on After Effects this is probably the "easiest" solution for complex animations (and cross platform!)

People also started sharing animations, you can find them here:
http://www.lottiefiles.com/


do you know how it's done?

The (1) video looks like they do some custom drawing.

  • Draw and fill a Path for the water, animate the top with some waves while it animates up, and sprinkle with some white spots.
  • Draw a the shape of the glass around it

This will involve creating a custom view and/or drawable and overriding onDraw(Canvas) Path.lineTo as well as some arc, cubic, or quad will do the trick...I'm neither a designer nor a vector specialist :)

I do not think this could easily be achieved by using animated vector drawables, but you might get it done by applying a path morph animation. (Also animated vector drawables are only supported on 21+ if I am not mistaken)

The (2) Video just animates a Path and wiggles a flag. This can be achieved by using AnimatedVectorDrawables (as e.g. this blog shows) and trimming the path beginnings/ends or again by doing custom drawing (and thus also supporting pre lolipop devices) by animating a Path, e.g. using PathMeasure.getSegment() to continuuously animate the path.

The (3) animation, as already pointed out by azizbekians answer is the first mentioned kind, just animating (moving and scaling) views.


Regarding the second example, only one idea came to my mind, and that's setting a Path and moving the View accordingly by passing it somehow after animate(). (2) Is this possible?

Moving views along a path is obviously possible, but then the view would move, and you would still need to find a way to draw the path following it, as explained above.

1
votes

I would like to suggest a library I've written a while ago (published recently), which allows you to create custom views and add animations to them.

You can find it here and a very simple demo here. While my demos frankly aren't that impressive, they demonstrate the power of the library and most importantly how to use it.

It works with Android's TimeInterpolator and any of its children (as-well as custom ones).

The animations are drawn entirely using Canvas, the only difference here is that you can get an animated value, which changes according to the TimeInterpolator as a function of time.

You can add as many animations as you would like to your View, control them independently of each-other. And on top of that, you are not limited to drawing non-animation things. There's a dedicated method for that (onDrawStatics).

-2
votes

For Your Custom view , i suggest you identify the Properties that you want to Animate on .

Let's say a ClockView, You After setting paint,stroke and so on in OnDraw()

You might wanna have AsyncMethods/Runnables which updates the Properties continuously in background , resulting in Frame by frame animation , but it isn't Fraame Animation actually , because you are animating only the properties of view (turn left) and not he tentire frame

There are dozens of examples out there, With little googling you can DIY