Enter animation using RecyclerView and LayoutAnimation Part 1: Lists

介紹
本教程我們將帶大家學(xué)習(xí)一個(gè)為RecyclerView添加初始動(dòng)畫(huà)的簡(jiǎn)單方法。實(shí)現(xiàn)這種動(dòng)畫(huà)有幾種辦法,比如:
實(shí)現(xiàn)一個(gè)自定義的ItemAnimator
在Adapter的onBindViewHolder()方法中添加動(dòng)畫(huà)
我將采用第三種辦法:LayoutAnimation。它非常簡(jiǎn)單,而且只需很少的代碼。值得注意的是雖然這篇文章是用RecyclerView來(lái)做的,但是LayoutAnimation可以用在任何ViewGroup上。
本文將講述:
為每個(gè)item定義一個(gè)動(dòng)畫(huà)
使用item的動(dòng)畫(huà)定義LayoutAnimation
在XML或者代碼中應(yīng)用LayoutAnimation
這是這個(gè)系列教程的第一篇,考慮的是RecyclerView為列表的場(chǎng)景(LinearLayoutManager)。雖然這里的方法用在GridLayoutManager的時(shí)候仍然有效,但是我們想為grid使用一個(gè)體驗(yàn)更好的動(dòng)畫(huà),留在第二部分講解。
本教程的demo源碼在這里,包含了List和Grid的例子:
https://github.com/patrick-iv/Enter-animation-demo
demo的apk在這里!
讓我們開(kāi)始吧
首先讓我們創(chuàng)建item的動(dòng)畫(huà),這里我們創(chuàng)建的是一個(gè)下落的動(dòng)畫(huà):

在res/anim/目錄下創(chuàng)建一個(gè) item_animation_fall_down.xml 文件并添加如下內(nèi)容:
<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>
上面定義的動(dòng)畫(huà)元素將會(huì)同時(shí)運(yùn)行,這里是對(duì)每個(gè)動(dòng)畫(huà)元素的解釋:
Translate Y -20% to 0%
在動(dòng)畫(huà)開(kāi)始前,把view向上移動(dòng)自身高度的20%,然后讓他下降到自己最終的位置。Alpha 0 to 1
從完全不可見(jiàn)慢慢過(guò)渡到完全可見(jiàn)。Scale X/Y 105% to 100%
放大到105%,然后縮小的實(shí)際大小。
定義LayoutAnimation
定義了item要運(yùn)行的動(dòng)畫(huà)之后,我們來(lái)定義把動(dòng)畫(huà)應(yīng)用到每個(gè)子view的layout animation。在 res/anim/ 新建一個(gè)名為layout_animation_fall_down.xml 的文件,添加如下代碼:
<?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”
定義item運(yùn)行的動(dòng)畫(huà)(資源)。android:delay=”15%"
為每個(gè)item動(dòng)畫(huà)添加一個(gè)延時(shí),基于item動(dòng)畫(huà)的duration。0%將導(dǎo)致所有item同步運(yùn)行,100%則讓前一個(gè)item的動(dòng)畫(huà)運(yùn)行完了下一個(gè)item才開(kāi)始動(dòng)畫(huà)。我們這里使用的是15%,表示item A的動(dòng)畫(huà)運(yùn)行了15%之后才開(kāi)始運(yùn)行B的動(dòng)畫(huà)。android:animationOrder="normal"
有三種選擇:normal, reverse 和 random。它可以控制布局元素運(yùn)行動(dòng)畫(huà)的順序。Normal是按照布局的自然順序( vertical: top to bottom, horizontal: left to right),Reverse跟Normal是相反的,Random則按照隨即的順序。
使用 LayoutAnimation
應(yīng)用LayoutAnimation可以通過(guò)代碼實(shí)現(xiàn),也可以通過(guò)XML實(shí)現(xiàn)。
java代碼
int resId = R.anim.layout_animation_fall_down;
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(ctx, resId);
recyclerview.setLayoutAnimation(animation);
xml
1. <android.support.v7.widget.RecyclerView
2. android:layout_width="match_parent"
3. android:layout_height="match_parent"
4. android:layoutAnimation="@anim/layout_animation_fall_down"
5. />
如果你想在數(shù)據(jù)變化的時(shí)候觸發(fā)動(dòng)畫(huà):
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();
}
Wrapping up
內(nèi)容就是這么多了。我覺(jué)得這是RecylerView從空到渲染完內(nèi)容時(shí)執(zhí)行動(dòng)畫(huà)的最佳方式。就如我剛才提到的,這種辦法適用于所有ViewGroup。
第二部分 講解在RecyclerView為grid的時(shí)候如何對(duì)animation order 進(jìn)行更多的控制,文章在這里:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0819/8397.html
別忘了本教程兩個(gè)部分的文章的代碼都在這里。
最后附兩個(gè)動(dòng)畫(huà):
附: Slide from right
item_animation_from_right.xml
<?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_slide_right.xml
<?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"
/>
這里要注意的是android:fromXDelta="100%p"中的p 不是筆誤。當(dāng)添加了p之后百分比就是基于parent的,所以這里其實(shí)代表向右移動(dòng)parent的整個(gè)寬度。
附: Slide from bottom
item_animation_from_bottom.xml
<?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"
/>
layout_animation_from_bottom.xml
<?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
/>