Enter animation using RecyclerView and LayoutAnimation Part 1: Lists
介紹
本教程我們將帶大家學(xué)習(xí)一個為RecyclerView添加初始動畫的簡單方法。實現(xiàn)這種動畫有幾種辦法,比如:
實現(xiàn)一個自定義的ItemAnimator
在Adapter的onBindViewHolder()方法中添加動畫
我將采用第三種辦法:LayoutAnimation。它非常簡單,而且只需很少的代碼。值得注意的是雖然這篇文章是用RecyclerView來做的,但是LayoutAnimation可以用在任何ViewGroup上。
本文將講述:
為每個item定義一個動畫
使用item的動畫定義LayoutAnimation
在XML或者代碼中應(yīng)用LayoutAnimation
這是這個系列教程的第一篇,考慮的是RecyclerView為列表的場景(LinearLayoutManager)。雖然這里的方法用在GridLayoutManager的時候仍然有效,但是我們想為grid使用一個體驗更好的動畫,留在第二部分講解。
本教程的demo源碼在這里,包含了List和Grid的例子:
https://github.com/patrick-iv/Enter-animation-demo
demo的apk在這里!
讓我們開始吧
首先讓我們創(chuàng)建item的動畫,這里我們創(chuàng)建的是一個下落的動畫:
在res/anim/目錄下創(chuàng)建一個 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>
上面定義的動畫元素將會同時運(yùn)行,這里是對每個動畫元素的解釋:
Translate Y -20% to 0%
在動畫開始前,把view向上移動自身高度的20%,然后讓他下降到自己最終的位置。Alpha 0 to 1
從完全不可見慢慢過渡到完全可見。Scale X/Y 105% to 100%
放大到105%,然后縮小的實際大小。
定義LayoutAnimation
定義了item要運(yùn)行的動畫之后,我們來定義把動畫應(yīng)用到每個子view的layout animation。在 res/anim/ 新建一個名為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)行的動畫(資源)。android:delay=”15%"
為每個item動畫添加一個延時,基于item動畫的duration。0%將導(dǎo)致所有item同步運(yùn)行,100%則讓前一個item的動畫運(yùn)行完了下一個item才開始動畫。我們這里使用的是15%,表示item A的動畫運(yùn)行了15%之后才開始運(yùn)行B的動畫。android:animationOrder="normal"
有三種選擇:normal, reverse 和 random。它可以控制布局元素運(yùn)行動畫的順序。Normal是按照布局的自然順序( vertical: top to bottom, horizontal: left to right),Reverse跟Normal是相反的,Random則按照隨即的順序。
使用 LayoutAnimation
應(yīng)用LayoutAnimation可以通過代碼實現(xiàn),也可以通過XML實現(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ù)變化的時候觸發(fā)動畫:
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)容就是這么多了。我覺得這是RecylerView從空到渲染完內(nèi)容時執(zhí)行動畫的最佳方式。就如我剛才提到的,這種辦法適用于所有ViewGroup。
第二部分 講解在RecyclerView為grid的時候如何對animation order 進(jìn)行更多的控制,文章在這里:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0819/8397.html
別忘了本教程兩個部分的文章的代碼都在這里。
最后附兩個動畫:
附: 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的,所以這里其實代表向右移動parent的整個寬度。
附: 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
/>