Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the current, new alternative to ViewAnimator?

Background

I have been using ViewAnimator/ViewSwitcher for a long time.

The most common use case I had was to switch from loading phase to content phase, or between phases of a wizard, and even having an error phase sometimes.

The problem

When I suggested adding a nice extension function to "android-ktx" repository (here), I was told:

ViewAnimator is not an API we actively recommend to animate views. It's based on the old animation system and we don't want to promote its use in this library.

What I've tried

I've looked at articles of ViewAnimator and ViewSwitcher, including the docs. It doesn't say there that it was replaced/deprecated, or that it's recommended to use something else instead.

The questions

  1. What has replaced ViewAnimator? Is he talking about transitions?

  2. What are the advantages and disadvantages compared to ViewAnimator?

  3. Given a ViewAnimator with some views, how would it be converted to the newer solution, including the switching between the states?

like image 985
android developer Avatar asked Feb 07 '18 10:02

android developer


People also ask

Is Viewflipper deprecated?

This constant was deprecated in API level 28. The view drawing cache was largely made obsolete with the introduction of hardware-accelerated rendering in API 11.

What is view animator?

In Android, ViewAnimator is a sub class of FrameLayout container that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views ( like TextView, ImageView or any layout). It is mainly useful to animate the views on screen.


1 Answers

I assume what Romain Guy means, is that ViewAnimator uses Animation API, whereas a newer API is considered Animator. See "How Property Animation Differs from View Animation" in the docs, where advantages and disadvantages of each APIs are mentioned as well as usage scenarios:

The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.

Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.

The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.

There is no straightforward way to "convert ViewAnimator to use newer approach" though, because it internally uses Animation API. As mentioned in the docs: "if view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system", that's why ViewAnimator is not deprecated.

like image 134
azizbekian Avatar answered Oct 20 '22 05:10

azizbekian