Add animation to the list using RecyclerView and Layout Animation

Keywords: Programming Android xml encoding

Add animation to the list using RecyclerView and Layout Animation

Bai Shixiu'er Collection was transferred from May to 14, 2018 Report

Here's how to use RecyclerView and Layout Animation to add animation to the list. There are three steps:

  1. Add animation to list items
  2. Define Layout Animation with Animation of List Items
  3. RecyclerView Applying Layout Animation Animation

The advantage of using Layout Animation to define animation is that it is defined separately and can be applied to any subclass of ViewGroup. Here is an example of RecyclerView.

Example effect

The example is a drop-down entry effect, as shown in Figure 1:

Define list item animation

Add a file item_animation_fall_down.xml under the res/anim/ path, which is used to define the animation of list items. Add the following:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="@integer/anim_duration_medium">

    <translate
        android:fromYDelta="-20%"
        android:toYDelta="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

    <scale
        android:fromXScale="105%"
        android:fromYScale="105%"
        android:toXScale="100%"
        android:toYScale="100%"
        android:pivotX="50%"
        android:pivotY="50%"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

</set>

Explain:

  • Translate Y-20% to 0%: Move the view up 20% before the animation starts and let it fall to its final position.
  • Alpha 0 to 1: Views are completely invisible from the beginning and appear slowly until they are fully visible.
  • Scale X/Y 105% to 100%: Scale is set to 105% to reduce it to final size. This will make it look like it's falling off and landing in the background.

Define Layout Animation

Use each animation defined above to define Layout Animation. Add the layout_animation_fall_down.xml file in res/anim/to define Layout Animation. The contents are as follows:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_animation_fall_down"
    android:delay="15%"
    android:animationOrder="normal"
    />
  • android:animation="@anim/item_animation_fall_down": defines which animation is applied to each item in the layout.
  • android:delay = "15%": Set the animation delay, 0% means that all projects are animated at the same time, and 100% means that each project completes its animation before starting the next project. Fifteen percent said that when the project animation reached 15 percent, it would start the next project animation.
  • android:animationOrder="normal": There are three types of animation sequence that control content: normal, reverse and random. Noral means that animation occurs in the natural order of the layout (vertical: top-down, horizontal: left-to-right). Reverse is the opposite of normal. Random means that animation appears randomly.

Applying Layout Animation

There are two ways to use Layout Animation: programming and xml.

Programming method:

int resId = R.anim.layout_animation_fall_down;
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(ctx, resId);
recyclerview.setLayoutAnimation(animation);

xml mode:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"                                        
    android:layoutAnimation="@anim/layout_animation_fall_down"
    />

If you need to change the data set, you can do this:

private void runLayoutAnimation(final RecyclerView recyclerView) {
    final Context context = recyclerView.getContext();
    final LayoutAnimationController controller =
            AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);

    recyclerView.setLayoutAnimation(controller);
    recyclerView.getAdapter().notifyDataSetChanged();
    recyclerView.scheduleLayoutAnimation();
}

Animation 2: Slide in from the right

Project Animation:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="@integer/anim_duration_long">

    <translate
        android:interpolator="@android:anim/decelerate_interpolator"
        android:fromXDelta="100%p"
        android:toXDelta="0"
        />

    <alpha
        android:fromAlpha="0.5"
        android:toAlpha="1"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        />

</set>

Layout Animation Animation

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_animation_from_right"
    android:delay="10%"
    android:animationOrder="normal"
    />

Animation 3: Slip in from the bottom

Project animation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="@integer/anim_duration_long">

    <translate
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromYDelta="50%p"
        android:toYDelta="0"
        />

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        />

</set>

Layout Animation Animation

<?xml version=1.0 encoding=utf-8?>
<layoutAnimation
    xmlns:android=http://schemas.android.com/apk/res/android
    android:animation=@anim/item_animation_from_bottom
    android:delay=15%
    android:animationOrder=normal
    />

Extract from: https://proandroiddev.com/enter-animation-use-recyclerview-and-layoutanimation-part-1-list-75a874a5d213

Posted by azaidi on Mon, 07 Oct 2019 19:12:51 -0700