14

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?

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
android developer
  • 114,585
  • 152
  • 739
  • 1,270

3 Answers3

3

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.

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • 1
    Is there maybe a sample of transitions though, to show how to switch between states ? For example, suppose you have a loading phase which has a TextView "loading info of person X" and an ImageView of this person, and in the loaded phase, you'd have the ImageView somewhere else, and a TextView (says "info about person X") in a different place on the screen, that the previous one should transition to. How would you do that? – android developer Feb 26 '18 at 14:19
0

Try to use AdapterViewAnimator. AdapterViewAnimator requires an adapter for child views, so it may be some difficult to use than ViewAnimator, but it has required methods from ViewAnimator class such as showNext(), setInAnimation(ObjectAnimator) and setOutAnimation(ObjectAnimator). Yes, you have to manually rewrite all the Animation to the ObjectAnimator.

netpork
  • 552
  • 7
  • 20
Nikolay
  • 1,429
  • 1
  • 13
  • 24
  • `AdaprterViewAnimator` is used only for `AdapterView` (ListView, GridView, and some others. What I've asked is more general, and what is the alternative to the old API. – android developer Feb 26 '18 at 14:14
0

I guess one possible alternative is using transitions of ConstraintLayout , as shown here .

To implement, it seems it has to use 2 similar layouts, with same ids for each view, and then you can switch between the phases, as such:

class MainActivity : AppCompatActivity() {
    private var show = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.circuit)
        backgroundImage.setOnClickListener {
            if(show)
                hideComponents() // if the animation is shown, we hide back the views
            else
                showComponents() // if the animation is NOT shown, we animate the views
        }
    }

    private fun showComponents(){
        show = true
        val constraintSet = ConstraintSet()
        constraintSet.clone(this, R.layout.circuit_detail)
        val transition = ChangeBounds()
        transition.interpolator = AnticipateOvershootInterpolator(1.0f)
        transition.duration = 1200
        TransitionManager.beginDelayedTransition(constraint, transition)
        constraintSet.applyTo(constraint)
    }

    private fun hideComponents(){
        show = false
        val constraintSet = ConstraintSet()
        constraintSet.clone(this, R.layout.circuit)
        val transition = ChangeBounds()
        transition.interpolator = AnticipateOvershootInterpolator(1.0f)
        transition.duration = 1200
        TransitionManager.beginDelayedTransition(constraint, transition)
        constraintSet.applyTo(constraint)
    }
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270