Android: Attribute Animation

Keywords: Android Attribute xml Java

Attribute animation is added by Android 3.0. In order to make up for the shortcomings of View animation, but below Android 3.0, although animation compatible library can be used, its essence is still View animation.

Disadvantages of View Animation
Single animation effect, rotation, translation, zooming
Image animation, without changing its own parameters, needs interactive view ing to expose defects
The object of action is constrained on the View

ValueAnimator class
First change the value, then assign the attribute to the object manually to achieve animation; it is indirect operation of the object attribute;

ValueAnimator operates on values

ValueAnimator.ofInt(int values)
ValueAnimator.ofFloat(float values)
ValueAnimator.ofObject(int values)  Need to customize the estimator

ObjectAnimator class
First change the value, then automatically assign the object's attributes to achieve animation; is to directly operate on the object's attributes;

ObjectAnimator inherits from the ValueAnimator class, that is, the underlying animation implementation mechanism is based on the ValueAnimator class.
ObjectAnimator classes are for arbitrary objects - arbitrary attribute values, not just for View objects

If you need to use ObjectAnimator class to achieve animation effect, then the object you need to manipulate must have set () & get () of that property.
Similarly, for the other three basic animation effects mentioned above, View also has setRotation(), getRotation(), setTranslationX(), getTranslationX(), setScaleY(), getScaleY(), and so on set () & get ().
The AnimatorSet class can be implemented in combination animation with XML settings / Java code settings

AnimatorSet.play(Animator anim): Play the current animation 
AnimatorSet.after(long delay): Delays existing animations by x milliseconds before execution 
AnimatorSet.with(Animator anim): Executing both existing and incoming animations
 AnimatorSet.after(Animator anim): Execute after inserting an existing animation into the incoming animation 
AnimatorSet.before(Animator anim): Execute an existing animation before inserting it into the incoming animation

Java Code Implementation

// Step 1: Set the animation effects that need to be combined 
ObjectAnimator translation = ObjectAnimator.ofFloat(mButton, "translationX", curTranslationX, 300,curTranslationX); // translating animation 
ObjectAnimator rotate = ObjectAnimator.ofFloat(mButton, "rotation", 0f, 360f); // Rotary animation 
ObjectAnimator alpha = ObjectAnimator.ofFloat(mButton, "alpha", 1f, 0f, 1f); // Transparency animation 
// Step 2: Create composite animation objects
 AnimatorSet animSet = new AnimatorSet(); 
// Step 3: Combine animations according to requirements 
animSet.play(translation).with(rotate).before(alpha); 
animSet.setDuration(5000); 
// Step 4: Start the animation
 animSet.start();

xml implementation

Create an animator resource folder under the value directory, where you create xml files
ordering is the way animation is played: together and sequentially represent two ways of executing simultaneously and sequentially

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially">
    <set android:ordering="together">
        <objectAnimator android:propertyName="translationX" android:valueFrom="0dp" android:valueTo="300dp" android:duration="3000" android:valueType="floatType"/>
        <objectAnimator android:propertyName="rotation" android:valueFrom="0" android:valueTo="360" android:duration="2000" android:valueType="floatType"/>
    </set>
    <set android:ordering="sequentially">
        <objectAnimator android:propertyName="alpha" android:valueFrom="1" android:valueTo="0" android:duration="500" android:valueType="floatType"/>
        <objectAnimator android:propertyName="alpha" android:valueFrom="0" android:valueTo="1" android:duration="500" android:valueType="floatType"/>
    </set>
</set>

Posted by gurhy on Fri, 13 Sep 2019 00:29:35 -0700