I usually project development necessary framework

  1. The strongest network request Net on Android
  2. The strongest list on Android (including StateLayout) BRV
  3. Android’s strongest default page StateLayout
  4. JSON and long text log printing tool LogCat
  5. Supports asynchronous and global customization of the toast tool Tooltip
  6. Develop the debugging window tool DebugKit
  7. One line of code creates the transparent StatusBar StatusBar

By this I mean Google’s encapsulation of commonly used animation effects. There are a lot of cool effects that can be achieved quickly with these packages.

LayoutAnimationController

Used to support list controls (RecyclerView and ListView) Item animation effect

Properties:

  1. animation

    Pass in the animation XML file to decide to load the animation

  2. The animation sequence

    There are three values: normal In normal order Reverse loads animations from the last control random in random order

  3. Delay time

    Millisecond value of the millisecond difference in load time for each entry

  4. Animation interpolator

Layout animation can be used in two ways:

  1. Reference animation in layout

Create the XML in the anim directory

<?xml version="1.0" encoding="utf-8"? >
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/animation_set"
    android:animationOrder="normal"
    android:delay="300">
</layoutAnimation>
Copy the code

Reference the XML animation in the layout XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:id="@+id/ll_root"
    android:gravity="center"
    android:layoutAnimation="@anim/animation_layout"
    >
Copy the code
  1. Reference animation in code
// Create an Animation object by loading the XML Animation Settings file;
Animation animation=AnimationUtils.loadAnimation(this, R.anim.list_anim);
/ / get a LayoutAnimationController object;
LayoutAnimationController mLayoutAnimationController = new LayoutAnimationController(animation);
// Set the display order of the controls;
mLayoutAnimationController.setOrder(LayoutAnimationController.ORDER_REVERSE);
// Set the control display interval;
mLayoutAnimationController.setDelay(1);
/ / for ListView LayoutAnimationController properties;
mList.setLayoutAnimation(mLayoutAnimationController);
Copy the code

Note that the layout animation is performed either in the onCreate method, or in other places you need to manually execute the invalidate() method of the layout object.

This function belongs to the animation effect under ViewGroup

void setLayoutAnimation (LayoutAnimationController controller)
Copy the code

LayoutTransitionController

I have a Crash situation. ExampleDemo cannot be run directly. The View parameter pointing to ObjectAnimator cannot be Null, and the Null pointer is abnormal. I’m not going to explain it because I haven’t solved it.

However, you can enable the default animation effect by adding the following properties to the ViewGroup in the XML layout

android:animateLayoutChanges="true"
Copy the code

ViewAnimationUtils

A reveal effect can also be thought of as a rounded corner change animation that controls a control. The ViewAnimationUtils class has only one method:

Animator createCircularReveal (View view, 
                int centerX, 
                int centerY, 
                float startRadius, 
                float endRadius)
Copy the code

// Create a circular display animation
Animator animator = ViewAnimationUtils.createCircularReveal(mBtnExtract, mBtnExtract.getWidth() / 2, mBtnExtract.getHeight() / 2, mBtnExtract.getWidth() / 4, mBtnExtract.getHeight() / 4);

animator.setInterpolator(new LinearInterpolator());
animator.setDuration(1000);
animator.start();
Copy the code

overridePendingTransition

Customize the animation XML file for entering the next Activity and exiting the current Activity without any compatibility concerns.

Note that it must be called after startActivity or finishd to be valid.

overridePendingTransition(int enterAnim, int exitAnim); // 0 indicates that no animation is displayed
Copy the code
  • Enter an animation on the enterAnim interface
  • ExitAnim exits the animation

ViewPropertyAnimator

The View class provides an animate() method to quickly and easily set commonly used animations

ViewPropertyAnimator animate (a)
Copy the code

ViewPropertyAnimator is a class that basically sets all properties of a View

Many of these methods have the same name with and without “By”. “By” equals relative property value, no absolute property value.

ViewPropertyAnimator translationX (float value)
ViewPropertyAnimator translationXBy (float value)
Copy the code

You can perform multiple animation effects simultaneously if you use chain calls.

view.animate()  
        .scaleX(1)
        .scaleY(1)
        .alpha(1);
Copy the code

The listener

ViewPropertyAnimator setListener (Animator.AnimatorListener listener)
Copy the code

The AnimatorListener contains four callbacks

// When the animation is cancelled
abstract void	onAnimationCancel(Animator animation)

// Whether the animation is cancelled or not, it ends with a callback
abstract void	onAnimationEnd(Animator animation)

default void	onAnimationEnd(Animator animation, 
                               boolean isReverse) // Whether to repeat

// call back when the animation repeats
abstract void	onAnimationRepeat(Animator animation) 

// Start animation
abstract void	onAnimationStart(Animator animation)

default void	onAnimationStart(Animator animation, 
                                 boolean isReverse) // Whether the animation repeats
Copy the code

There is also a one-time callback that will not be called if the ViewPropertyAnimator object executes the animation again. And withEndAction() does not call back when the animation is cancelled. This is different from onAnimationEnd().

ViewPropertyAnimator withEndAction (Runnable runnable)

ViewPropertyAnimator withStartAction (Runnable runnable)
Copy the code

update

The callback is executed approximately every 10 milliseconds

ViewPropertyAnimator setUpdateListener (ValueAnimator.AnimatorUpdateListener listener)
Copy the code

DynamicAnimation

DynamicAnimation is an abstract animation based on physical motion effects that appears in API25. Officially, only SpringAnimation inherits this class. The advantage of using SpringAnimation is that the animation is less rigid, and one could argue that the animation interpolator would work as well. However, since the animation interpolator involves the coordinate axis algorithm, Google created DynamicAnimation in order to enable more people to conveniently optimize their animation effects.

The official instructions

The following is an official example


SpringForce spring = new SpringForce(0)
  .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY)
  .setStiffness(SpringForce.STIFFNESS_LOW);

final SpringAnimation anim = new SpringAnimation(mImag, DynamicAnimation.SCALE_Y)
  .setMinValue(0).setSpring(spring).setStartValue(1);
anim.start();
Copy the code

SpringForce

Set by SpringAnimation method

SpringAnimation setSpring (SpringForce force)
Copy the code

Alternatively, you can get an instance of SpringForce directly from SpringAnimation without creating it

SpringForce getSpring (a)
Copy the code

Is to control the strength of SpringAnimation and control three elastic characteristics respectively

  • The damping ratio
  • Degree of stiffness

Create constructor

SpringForce (float finalPosition) // Final position
Copy the code

SpringForce can determine the final value of the animation, but not the starting value (the default starting value is the current property value). The starting value can also be set by SpringAnimation.

Damping ratio setting (damping refers to swing times)

SpringForce setDampingRatio (float dampingRatio)
Copy the code

SpringForce offers four internal damping ratios:

public static final float DAMPING_RATIO_HIGH_BOUNCY = 0.2 f;  
 
public static final float DAMPING_RATIO_MEDIUM_BOUNCY = 0.5 f; / / the default value
 
public static final float DAMPING_RATIO_LOW_BOUNCY = 0.75 f; 
 
public static final float DAMPING_RATIO_NO_BOUNCY = 1f;
Copy the code

The stiffness value is specified in the swing time

SpringForce setStiffness (float stiffness)
Copy the code

SpringForce also provides four stiffness values:

public static final float STIFFNESS_HIGH = 10_000f;
 
public static final float STIFFNESS_MEDIUM = 1500f; / / the default value
 
public static final float STIFFNESS_LOW = 200f;
 
public static final float STIFFNESS_VERY_LOW = 50f;
Copy the code

Spring’s constructor

SpringAnimation (View v,  // Target view
                DynamicAnimation.ViewProperty property) // Attribute value, which will be explained later

SpringAnimation (View v, 
                DynamicAnimation.ViewProperty property, 
                float finalPosition)
Copy the code

ViewProperty is an abstract class inside

It can be seen from the source that the main function is to set the property value

DynamicAnimation provides a set of default viewProperties

TRANSLATION_X
TRANSLATION_Y
TRANSLATION_Z
SCALE_X
SCALE_Y
ROTATION
ROTATION_X
ROTATION_Y
X
Y
Z
ALPHA
SCROLL_X
SCROLL_Y
Copy the code

SpringAnimation can also change the final position while moving

void animateToFinalPosition (float finalPosition)
Copy the code

AnimatedStateListDrawable

Note the distinction between StateListAnimator and StateListAnimator, restricting api21;

StateListAnimator is the

tag that changes the item from Drawable to animation

<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item
        android:id="@+id/on"
        android:state_activated="true">
        <bitmap
            android:src="@drawable/ic_heart_100"/>
    </item>
 
    <item
        android:id="@+id/off">
        <bitmap
            android:src="@drawable/ic_heart_0"/>
    </item>
 
    <transition
        android:fromId="@+id/on"
        android:toId="@+id/off"
        android:drawable="@drawable/animation_emptying">
    </transition>
 
    <transition
        android:fromId="@id/off"
        android:toId="@id/on"
        android:drawable="@drawable/animation_filling">
    </transition>
 
</animated-selector>
Copy the code

Item can specify the starting and ending bitmap and the triggering state

Transition can specify what animation to perform when the state is triggered

Read my other articles for more animations

  • Vector animation
  • Animated transitions
  • ViewAnimator