Android開發(fā)MotionLayout做動(dòng)畫

<?xml version="1.0" encoding="utf-8"?>

<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">

<Transition
    android:id="@+id/first_half"
    motion:constraintSetEnd="@id/top"
    motion:constraintSetStart="@id/center"
    motion:duration="800">
    <KeyFrameSet>
        <KeyPosition
            motion:framePosition="40"
            motion:motionTarget="@id/tv_profit_num"
            motion:keyPositionType="deltaRelative"
            motion:percentY="0.1"/>
        <KeyPosition
            motion:framePosition="70"
            motion:motionTarget="@id/tv_profit_num"
            motion:keyPositionType="deltaRelative"
            motion:percentY="0.3"/>
        <KeyAttribute
            motion:framePosition="90"
            motion:motionTarget="@id/tv_profit_num"
            motion:keyPositionType="deltaRelative"
            motion:percentY="0.6"
            android:alpha="1"/>
    </KeyFrameSet>
</Transition>

<Transition
    android:id="@+id/all"
    motion:constraintSetEnd="@id/top"
    motion:constraintSetStart="@id/lower"
    motion:duration="250">
    <KeyFrameSet>
        <KeyAttribute
            motion:framePosition="50"
            motion:motionTarget="@id/tv_profit_num"
            motion:keyPositionType="parentRelative"
            motion:percentY="0.5"
            android:alpha="1"/>
    </KeyFrameSet>
</Transition>

<Transition
    android:id="@+id/lower_half"
    motion:constraintSetEnd="@id/center"
    motion:constraintSetStart="@id/lower"
    motion:duration="800">
    <KeyFrameSet>
        <KeyAttribute
            motion:framePosition="10"
            motion:motionTarget="@id/tv_profit_num"
            motion:keyPositionType="deltaRelative"
            android:alpha="1"
            motion:percentY="0.4"/>
        <KeyPosition
            motion:framePosition="30"
            motion:motionTarget="@id/tv_profit_num"
            motion:keyPositionType="deltaRelative"
            motion:percentY="0.7"/>
        <KeyPosition
            motion:framePosition="60"
            motion:motionTarget="@id/tv_profit_num"
            motion:keyPositionType="deltaRelative"
            motion:percentY="0.9"/>
    </KeyFrameSet>
</Transition>

<ConstraintSet android:id="@+id/center">
    <Constraint
        android:id="@+id/tv_profit_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>
</ConstraintSet>

<ConstraintSet android:id="@+id/top">
    <Constraint
        android:id="@+id/tv_profit_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>

<ConstraintSet android:id="@+id/lower">
    <Constraint
        android:id="@+id/tv_profit_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0"
        motion:layout_constraintLeft_toLeftOf="parent"
        motion:layout_constraintRight_toRightOf="parent"
        motion:layout_constraintBottom_toBottomOf="parent" />
</ConstraintSet>
</MotionScene>

要切換Transition侨核,它需要定義在同一個(gè)切換文件里面圣絮。如上面代碼祈惶。

切換動(dòng)畫時(shí)需要調(diào)transitionToStart()

如下面代碼:

        motionLayout?.run{
                            transitionToStart()
                            setTransition(if(animationType == 0) R.id.all else R.id.lower_half)
                            transitionToEnd()
                        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末捧请,一起剝皮案震驚了整個(gè)濱河市凡涩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌疹蛉,老刑警劉巖活箕,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異可款,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)筋讨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門摸恍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人立镶,你說我怎么就攤上這事∶拿剑” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵变泄,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我妨蛹,道長(zhǎng)晴竞,這世上最難降的妖魔是什么蛙卤? 我笑而不...
    開封第一講書人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任颤难,我火速辦了婚禮,結(jié)果婚禮上行嗤,老公的妹妹穿的比我還像新娘垛耳。我一直安慰自己栅屏,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開白布护奈。 她就那樣靜靜地躺著哥纫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蛀骇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評(píng)論 1 297
  • 那天简僧,我揣著相機(jī)與錄音建椰,去河邊找鬼偏灿。 笑死髓抑,一個(gè)胖子當(dāng)著我的面吹牛橄抹,可吹牛的內(nèi)容都是我干的况木。 我是一名探鬼主播咐蚯,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼湿诊!你這毒婦竟也來了狱杰?” 一聲冷哼從身側(cè)響起厅须,我...
    開封第一講書人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎朗和,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體千埃,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡忆植,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年皿曲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了吴侦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡备韧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出织堂,到底是詐尸還是另有隱情,我是刑警寧澤附较,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布潦俺,位于F島的核電站,受9級(jí)特大地震影響事示,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜肖爵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望冀自。 院中可真熱鬧秒啦,春花似錦熬粗、人聲如沸帝蒿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至答渔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間沼撕,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工务豺, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蚪燕。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓奔浅,卻偏偏與公主長(zhǎng)得像馆纳,于是被迫代替她去往敵國(guó)和親汹桦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

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