隨著RecycleView的普及運(yùn)用狂塘,它越來越多的逼格展示在我們面前谎碍,下面我來為RecycleView添加入場動畫隆敢。
在Android動畫<第一篇>:視圖動畫這篇博客上提到過布局動畫(LayoutAnimation)奉瘤,這個動畫同樣可以運(yùn)用在RecycleView翠忠,使RecycleView的Item實(shí)現(xiàn)逼格的入場動畫。
效果如下:
核心思想是:給RecycleView添加一個布局動畫
該動畫的代碼如下:
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"/>
有關(guān)layoutAnimation標(biāo)簽下的屬性的解釋:
android:delay 表示動畫播放的延時鬓照,既可以是百分比熔酷,也可以是 float 小數(shù)。
android:animationOrder 表示動畫的播放順序豺裆,有三個取值 normal(順序)拒秘、reverse(反序)、random(隨機(jī))臭猜。
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);
就可以轉(zhuǎn)成GridLayout布局羹应,那么在這種布局下,使用和上面一樣的入場動畫效果如下:
如圖次屠,展示一個從左到右入場的動畫园匹,該動畫每個Item的動畫時間是2s,layoutAnimation中設(shè)置的延遲時間delay是0.2劫灶,假設(shè)Item的動畫時間用animTime表示裸违,這里的延遲時間比例是0.2,延遲時間的計(jì)算方式如下:
delay = animTime * 0.2 = 2000 * 0.2 = 400ms
layoutAnimation中delay屬性值為0.2本昏,也可以設(shè)置成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%"/>
說到這里,我們會發(fā)現(xiàn)一個問題涌穆,Item是一個一個展示的怔昨,這樣花費(fèi)了大量的時間,有人說蒲犬,可以減小動畫時間和動畫的延遲時間朱监,那么,行原叮,我來縮短動畫時間:(將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"/>
動畫效果如下:
除了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"/>
有關(guān)gridLayoutAnimation 標(biāo)簽下的屬性的解釋:
android:rowDelay表示動畫播放的行延時悦荒,既可以是百分比,也可以是 float 小數(shù)嘹吨。
android:columnDelay表示動畫播放的列延時搬味,既可以是百分比,也可以是 float 小數(shù)蟀拷。
android:animationOrder 表示動畫的播放順序碰纬,有三個取值 normal(順序)、reverse(反序)问芬、random(隨機(jī))悦析。
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>
其效果如下:
這里稍微關(guān)注一下direction
屬性骑歹,從上圖效果可以看出direction
的屬性是top_to_bottom|left_to_right
,其余兩個取值我想沒必要多說了墨微。
[本章完...]