RecycleView 入場動畫

Enter animation using RecyclerView and LayoutAnimation Part 1: Lists

1-Y7zsNHV3Y9PnZ1uayYMsMg.gif

介紹

本教程我們將帶大家學(xué)習(xí)一個為RecyclerView添加初始動畫的簡單方法。實現(xiàn)這種動畫有幾種辦法,比如:

  • 實現(xiàn)一個自定義的ItemAnimator

  • 在Adapter的onBindViewHolder()方法中添加動畫

我將采用第三種辦法:LayoutAnimation。它非常簡單,而且只需很少的代碼。值得注意的是雖然這篇文章是用RecyclerView來做的,但是LayoutAnimation可以用在任何ViewGroup上。

本文將講述:

  1. 為每個item定義一個動畫

  2. 使用item的動畫定義LayoutAnimation

  3. 在XML或者代碼中應(yīng)用LayoutAnimation

這是這個系列教程的第一篇,考慮的是RecyclerView為列表的場景(LinearLayoutManager)。雖然這里的方法用在GridLayoutManager的時候仍然有效,但是我們想為grid使用一個體驗更好的動畫,留在第二部分講解。

https://medium.com/@patrick_iv/enter-animation-using-recyclerview-and-layoutanimation-part-2-grids-688829b1d29b

本教程的demo源碼在這里,包含了List和Grid的例子:

https://github.com/patrick-iv/Enter-animation-demo

demo的apk在這里!

讓我們開始吧

首先讓我們創(chuàng)建item的動畫,這里我們創(chuàng)建的是一個下落的動畫:

1-lx2_ejIZz7bOhgw9ewKFsA.gif

在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
 />
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,578評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,701評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,691評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,974評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,694評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,026評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,015評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,193評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,719評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,442評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,668評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,151評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,846評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,255評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,592評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,394評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內(nèi)容