Android animation. Part 1. Animator

in android •  7 years ago  (edited)

In this series of articles I want to talk about how you can use various animations in android development. This article is about the class Animator.

Simple animation. Animator - the first article
Simple animation. State View - second article
Vector animation. Standard solutions – the third article
VectorDrawable animation. Manage animation. Hardcore – the fourth article

If your task is only to move an object, change its size or its transparency, then you can use the class Animator and its heirs. Using them, you can animate the change of properties of various views.

Abstract class Animator has several successors:
ValueAnimator — the base class of animation, with which you can animate any property.
ObjectAnimator is a simplified interface for animating View properties. Has a list of different properties that we can change. The documentation specifies: offset relative to placement in layout (translationX and translationY), rotation (rotationX and rotationY), center displacement for rotation (pivotX and pivotY), scaling (scaleX and scaleY), location relative to the screen bounds from the upper left corner (x and y), transparency (alpha).
There is another convenient interface for the Animation View. It is not inherited from Animator, however it can be easily used through view.animate () - ViewPropertyAnimator.

Let's practice!
You can create an animation in the code:

ObjectAnimator animationX = ObjectAnimator.ofFloat(imageView, "scaleX", 1F);
ObjectAnimator animationY = ObjectAnimator.ofFloat(imageView, "scaleY", 1F);
AnimatorSet set = new AnimatorSet();
set.play(animationX).with(animationY);
set.setDuration(3000);
set.start();

Or so:

imageView.animate().setDuration(3000).scaleX(1).scaleY(1);

And it is possible and in xml:
Create a file show_animator.xml in res / animator in which we save this code:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">
    <objectAnimator
        android:duration="3000"
        android:propertyName="scaleX"
        android:valueTo="1"
        android:valueType="floatType"
       android:interpolator="@android:anim/decelerate_interpolator"/>

    <objectAnimator
        android:duration="3000"
        android:propertyName="scaleY"
        android:valueTo="1"
        android:valueType="floatType"
       android:interpolator="@android:anim/decelerate_interpolator"/>
</set>

Now let's run it in the code:

Animator set = AnimatorInflater.loadAnimator(this, R.animator.show_animator);
set.setTarget(imageView);
set.start();

One result in all cases:
ezgif.com-crop.gif

Excellent! We changed the scale of the picture with you - scaleX and scaleY. Also we were able to set the animation speed - 3s (duration - 3000 ms). It was not difficult at all, try it yourself now.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Very Helpful, Thanks

I'm glad that it was useful