白詩秀兒 收藏於2018-05-14 轉藏1次舉報html
這裏講解下使用RecyclerView和LayoutAnimation給列表添加進入動畫。分爲三個步驟:android
使用LayoutAnimation定義動畫的好處是,它是單獨定義,能夠應用於任何ViewGroup的子類。這裏是以RecyclerView爲示例。web
示例是一個向下掉的進入效果,如圖:
編程
在res/anim/路徑下添加文件item_animation_fall_down.xml,它用來定義列表項的動畫。添加內容以下:app
<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>
說明:dom
使用上面定義的每一項的動畫來定義LayoutAnimation。在res/anim/添加layout_animation_fall_down.xml文件,它用來定義LayoutAnimation。內容以下:webapp
<?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" />
有兩種方式使用LayoutAnimation:編程方式和xml方式。佈局
編程方式:動畫
int resId = R.anim.layout_animation_fall_down; LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(ctx, resId); recyclerview.setLayoutAnimation(animation);
xml方式:spa
<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:layoutAnimation="@anim/layout_animation_fall_down" />
若是須要更改數據集,能夠這樣作:
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(); }
項目動畫:
<?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>
LayoutAnimation動畫
<?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" />
項目動畫
<?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>
LayoutAnimation動畫
<?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 />
摘錄自:https://proandroiddev.com/enter-animation-using-recyclerview-and-layoutanimation-part-1-list-75a874a5d213