RecyclerView<第十篇>:添加入場動畫

隨著RecycleView的普及運(yùn)用狂塘,它越來越多的逼格展示在我們面前谎碍,下面我來為RecycleView添加入場動畫隆敢。

Android動畫<第一篇>:視圖動畫這篇博客上提到過布局動畫(LayoutAnimation)奉瘤,這個動畫同樣可以運(yùn)用在RecycleView翠忠,使RecycleView的Item實(shí)現(xiàn)逼格的入場動畫。

效果如下:

12.gif

核心思想是:給RecycleView添加一個布局動畫

圖片.png

該動畫的代碼如下:

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布局羹应,那么在這種布局下,使用和上面一樣的入場動畫效果如下:

13.gif

如圖次屠,展示一個從左到右入場的動畫园匹,該動畫每個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"/>

動畫效果如下:

14.gif

除了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>

其效果如下:

15.gif

這里稍微關(guān)注一下direction屬性骑歹,從上圖效果可以看出direction的屬性是top_to_bottom|left_to_right,其余兩個取值我想沒必要多說了墨微。

[本章完...]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末道媚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子欢嘿,更是在濱河造成了極大的恐慌衰琐,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炼蹦,死亡現(xiàn)場離奇詭異,居然都是意外死亡狸剃,警方通過查閱死者的電腦和手機(jī)掐隐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钞馁,“玉大人虑省,你說我怎么就攤上這事∩耍” “怎么了探颈?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長训措。 經(jīng)常有香客問我伪节,道長光羞,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任怀大,我火速辦了婚禮纱兑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘化借。我一直安慰自己潜慎,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布蓖康。 她就那樣靜靜地躺著铐炫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蒜焊。 梳的紋絲不亂的頭發(fā)上倒信,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音山涡,去河邊找鬼堤结。 笑死,一個胖子當(dāng)著我的面吹牛鸭丛,可吹牛的內(nèi)容都是我干的竞穷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼鳞溉,長吁一口氣:“原來是場噩夢啊……” “哼瘾带!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起熟菲,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤看政,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后抄罕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體允蚣,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年呆贿,在試婚紗的時候發(fā)現(xiàn)自己被綠了嚷兔。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡做入,死狀恐怖冒晰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情竟块,我是刑警寧澤壶运,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站浪秘,受9級特大地震影響蒋情,放射性物質(zhì)發(fā)生泄漏埠况。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一恕出、第九天 我趴在偏房一處隱蔽的房頂上張望询枚。 院中可真熱鬧,春花似錦浙巫、人聲如沸金蜀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽渊抄。三九已至,卻和暖如春丧裁,著一層夾襖步出監(jiān)牢的瞬間护桦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工煎娇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留二庵,地道東北人。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓缓呛,卻偏偏與公主長得像催享,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子哟绊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

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