RecyclerView<第十篇>:添加入场动画

随着RecycleView的普及运用,它越来越多的逼格展示在我们面前,下面我来为RecycleView添加入场动画。

Android动画<第一篇>:视图动画这篇博客上提到过布局动画(LayoutAnimation),这个动画同样可以运用在RecycleView,使RecycleView的Item实现逼格的入场动画。

效果如下:

12.gif

核心思想是:给RecycleView添加一个布局动画

图片.png

该动画的代码如下:

anim_layout.xml

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

有关layoutAnimation标签下的属性的解释:

android:delay 表示动画播放的延时,既可以是百分比,也可以是 float 小数。
android:animationOrder 表示动画的播放顺序,有三个取值 normal(顺序)、reverse(反序)、random(随机)。
android:animation 指向了子控件所要播放的动画。

animation_popup_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="2000"
        android:fromXDelta="-100%"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0"/>
    <alpha
        android:duration="2000"
        android:fromAlpha="0"
        android:toAlpha="1"
        />
</set>

那么,如果是GridLayout的布局呢?

我们只需要将

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RecycleViewActivity.this);
    recycleview.setLayoutManager(linearLayoutManager);

改成

    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
    recycleview.setLayoutManager(gridLayoutManager);

就可以转成GridLayout布局,那么在这种布局下,使用和上面一样的入场动画效果如下:

13.gif

如图,展示一个从左到右入场的动画,该动画每个Item的动画时间是2s,layoutAnimation中设置的延迟时间delay是0.2,假设Item的动画时间用animTime表示,这里的延迟时间比例是0.2,延迟时间的计算方式如下:

delay = animTime * 0.2 = 2000 * 0.2 = 400ms

layoutAnimation中delay属性值为0.2,也可以设置成20%,如下:

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

说到这里,我们会发现一个问题,Item是一个一个展示的,这样花费了大量的时间,有人说,可以减小动画时间和动画的延迟时间,那么,行,我来缩短动画时间:(将2000ms改成200ms)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fromXDelta="-100%"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0"/>
    <alpha
        android:duration="200"
        android:fromAlpha="0"
        android:toAlpha="1"
        />
</set>


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

动画效果如下:

14.gif

除了layoutAnimation之外,官方还提供了gridLayoutAnimation动画,使用gridLayoutAnimation动画必须自定义RecyclerView,否则崩溃。

自定义RecyclerView如下:

public class GridRecyclerView extends RecyclerView {
    public GridRecyclerView(@NonNull Context context) {
        super(context);
    }

    public GridRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GridRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params,
                                                   int index, int count) {
        final LayoutManager layoutManager = getLayoutManager();
        if (getAdapter() != null && layoutManager instanceof GridLayoutManager){

            GridLayoutAnimationController.AnimationParameters animationParams =
                    (GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;

            if (animationParams == null) {
                // If there are no animation parameters, create new once and attach them to
                // the LayoutParams.
                animationParams = new GridLayoutAnimationController.AnimationParameters();
                params.layoutAnimationParameters = animationParams;
            }

            // Next we are updating the parameters

            // Set the number of items in the RecyclerView and the index of this item
            animationParams.count = count;
            animationParams.index = index;

            // Calculate the number of columns and rows in the grid
            final int columns = ((GridLayoutManager) layoutManager).getSpanCount();
            animationParams.columnsCount = columns;
            animationParams.rowsCount = count / columns;

            // Calculate the column/row position in the grid
            final int invertedIndex = count - 1 - index;
            animationParams.column = columns - 1 - (invertedIndex % columns);
            animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;

        } else {
            // Proceed as normal if using another type of LayoutManager
            super.attachLayoutAnimationParameters(child, params, index, count);
        }
    }
}

它同样需要在布局上添加layoutAnimation:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccccc">


    <com.zyc.hezuo.animationdemo.GridRecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutAnimation="@anim/anim_layout"/>
</RelativeLayout>

anim_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/animation_popup_enter"
    android:animationOrder="normal"
    android:rowDelay="15%"
    android:columnDelay="15%"
    android:direction="top_to_bottom"/>

有关gridLayoutAnimation 标签下的属性的解释:

android:rowDelay表示动画播放的行延时,既可以是百分比,也可以是 float 小数。
android:columnDelay表示动画播放的列延时,既可以是百分比,也可以是 float 小数。
android:animationOrder 表示动画的播放顺序,有三个取值 normal(顺序)、reverse(反序)、random(随机)。
android:animation 指向了子控件所要播放的动画。
android:direction表示Item展示的方向,它有四个取值top_to_bottom(从上往下)、left_to_right(从左往右)、right_to_left(从右往左)、bottom_to_top(从下往上)

animation_popup_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/anticipate_overshoot">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:fromYDelta="100%"
        android:toXDelta="0"
        android:toYDelta="0"/>
    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1"
        />
</set>

其效果如下:

15.gif

这里稍微关注一下direction属性,从上图效果可以看出direction的属性是top_to_bottom|left_to_right,其余两个取值我想没必要多说了。

[本章完...]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容