ConstraintLayout動畫實現(xiàn)

前言
最近在無意中看到一篇關(guān)于ConstraintLayout的文章,文章著重介紹了最新的ConstraintLayout支持實現(xiàn)動畫效果们镜。于是我參照文章的內(nèi)容横朋,實現(xiàn)的動畫效果如下:

1.gif

是不是炫酷屌炸天!關(guān)鍵的是那么復(fù)雜的動畫效果僅僅用幾行代碼4都住!S邸卿啡!
實現(xiàn)步驟 1.把我們的ConstaintLayout的版本是升級到1.0.0-beta4’
<pre>compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4
</pre>
2.添加transition的依賴。(這個很關(guān)鍵菱父,不能漏掉哦>蹦取)
<pre>
compile 'com.android.support:transition:25.1.0'
</pre>

3.新建兩個布局文件剑逃,(注意要用ConstarintLayout布局),一個為執(zhí)行動畫前的布局官辽,另一個是執(zhí)行動畫后的布局蛹磺。如我的布局文件
<pre>
執(zhí)行動畫前的布局<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:id="@+id/constraint_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Apply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_apply" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" app:layout_constraintRight_toLeftOf="@+id/guideline2" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" /> <Button android:text="Reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_reset" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="16dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="16dp" app:layout_constraintLeft_toLeftOf="@+id/guideline2" /> <android.support.constraint.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guideline2" android:orientation="vertical" app:layout_constraintGuide_percent="0.5052083" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="194dp" /> <ImageView android:layout_width="96dp" android:layout_height="96dp" app:srcCompat="@mipmap/ic_launcher" android:id="@+id/iv_picture" android:layout_marginStart="16dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginLeft="16dp" android:layout_marginTop="56dp" app:layout_constraintTop_toTopOf="parent" /> <CheckBox android:text="CheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:layout_editor_absoluteY="23dp" android:id="@+id/checkBox" android:layout_marginStart="16dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginLeft="16dp" /> <TextView android:text="TextView1234444444444443232424334444344447758475353\nfiefjifhfehfuerieurieurif12fjeifjiejfeiffjeifjfjeijfifjeshfehfeufjieifjifjiejffjeifjifjifjiefeifjeifjeijfeifjeifjeijfiejfeifjeifjeifjeijfeifjeifjeifjeifjeifjeifjeifjeifjeifjiejfiefeijfeifjiejfeijfeijfiejfiejfiefjiefjiefjeifjeifjeifjeijfeijfeifjeijfiejfeifjeifjeifjeifji" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/tv_content" android:layout_marginStart="32dp" app:layout_constraintTop_toTopOf="@+id/checkBox" app:layout_constraintLeft_toRightOf="@+id/checkBox" android:layout_marginLeft="32dp" android:layout_marginEnd="16dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="16dp" app:layout_constraintBottom_toTopOf="@+id/guideline3" android:layout_marginBottom="8dp" /> <android.support.constraint.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guideline3" android:orientation="horizontal" tools:layout_editor_absoluteY="154dp" tools:layout_editor_absoluteX="0dp" app:layout_constraintGuide_end="357dp" /> <Button android:text="Button" android:layout_width="368dp" android:layout_height="wrap_content" android:id="@+id/btn_content" android:layout_marginTop="64dp" app:layout_constraintTop_toBottomOf="@+id/tv_content" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" /></android.support.constraint.ConstraintLayout>
1</pre>
<pre>
執(zhí)行動畫后的布局<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_margin="8dp" android:text="Apply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_apply" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" app:layout_constraintRight_toLeftOf="@+id/guideline2" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" /> <Button android:layout_margin="8dp" android:text="Reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_reset" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="16dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="16dp" app:layout_constraintLeft_toLeftOf="@+id/guideline2" /> <android.support.constraint.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guideline2" android:orientation="vertical" app:layout_constraintGuide_percent="0.5338542" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="205dp" /> <ImageView android:layout_width="96dp" android:layout_height="96dp" app:srcCompat="@mipmap/ic_launcher" android:id="@+id/iv_picture" tools:layout_editor_absoluteY="24dp" android:layout_marginEnd="16dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="16dp" android:layout_marginStart="16dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginLeft="16dp" /> <CheckBox android:text="CheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/checkBox" tools:layout_editor_absoluteY="7dp" android:layout_marginEnd="16dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="16dp" app:layout_constraintLeft_toLeftOf="@+id/guideline2" app:layout_constraintHorizontal_bias="0.66" /> <TextView android:text="TextView124444344447758475353\nfiefjifhfehfuerieurieurif12fjeifjiejfeiffjeifjfjeijfifjeshfehfeufjieifjifjiejffjeifjifjifjiefeifjeifjeijfeifjeifjeijfiejfeifjeifjeifjeijfeifjeifjeifjeifjeifjeifjeifjeifjeifjiejfiefeijfeifjiejfeijfeijfiejfiejfiefjiefjiefjeifjeifjeifjeijfeijfeifjeijfiejfeifjeifjeifjeifji" android:layout_width="226dp" android:layout_height="wrap_content" android:id="@+id/tv_content" app:layout_constraintRight_toLeftOf="@+id/guideline2" app:layout_constraintLeft_toLeftOf="@+id/guideline2" app:layout_constraintTop_toTopOf="@+id/guideline3" /> <android.support.constraint.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guideline3" android:orientation="horizontal" tools:layout_editor_absoluteY="130dp" tools:layout_editor_absoluteX="0dp" app:layout_constraintGuide_end="381dp" /> <Button android:layout_margin="16dp" android:text="Button" android:layout_width="0dp" android:layout_height="120dp" android:id="@+id/btn_content" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_content" app:layout_constraintHorizontal_bias="0.0" /></android.support.constraint.ConstraintLayout>
</pre>
注意:務(wù)必保持兩個布局文件中對應(yīng)控件的Id一致,要不然沒有動畫效果
4.在Activity中設(shè)置Java代碼同仆。如下:

360截圖20170211213655202.jpg

其實這里最為關(guān)鍵的是ConstraintSet這個類的應(yīng)用 萤捆。關(guān)于這個類的介紹請戳ConstaintSet,就是類撐起ConstaintLayout實現(xiàn)動畫效果的一片天俗批。俗或。。哈哈哈哈7龆啤蕴侣!

360截圖20170211213949183.jpg

首先我們創(chuàng)建兩個變量mConstraintSet1,(用于恢復(fù)正常狀態(tài))mConstraintSet2(用于執(zhí)行動畫狀態(tài)),初始化變量。 然后分別調(diào)用它們clone()的方法臭觉,把ConstraintLayout的布局內(nèi)容克隆到ConstraintSet中。 需要注意的是clone(this,R.layout.animation_execute)這個方法辱志,它的第二個參數(shù)為你要執(zhí)行動畫后的布局哦蝠筑。。揩懒。什乙。
最后我們在回調(diào)函數(shù)中執(zhí)行如下代碼即可。
執(zhí)行動畫代碼:

TransitionManager.beginDelayedTransition(constraintLayout); mConstraintSet2.applyTo(constraintLayout);

還原正常狀態(tài)代碼
TransitionManager.beginDelayedTransition(constraintLayout); mConstraintSet1.applyTo(constraintLayout);

到此已球,運行代碼就可以看到動畫效果了臣镣。 這就是ConstraintSet的簡單用法,它還可以為ConstaintLayout實現(xiàn)許多炫酷的動畫效果智亮,如果你有興趣的話忆某,可以戳這里ConstraintSet實現(xiàn)動畫效果查看更多內(nèi)容。阔蛉。弃舒。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末状原,一起剝皮案震驚了整個濱河市聋呢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌颠区,老刑警劉巖削锰,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異毕莱,居然都是意外死亡器贩,警方通過查閱死者的電腦和手機(jī)颅夺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來磨澡,“玉大人碗啄,你說我怎么就攤上這事∥壬悖” “怎么了稚字?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長厦酬。 經(jīng)常有香客問我胆描,道長,這世上最難降的妖魔是什么仗阅? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任昌讲,我火速辦了婚禮,結(jié)果婚禮上减噪,老公的妹妹穿的比我還像新娘短绸。我一直安慰自己,他們只是感情好筹裕,可當(dāng)我...
    茶點故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布醋闭。 她就那樣靜靜地躺著,像睡著了一般朝卒。 火紅的嫁衣襯著肌膚如雪证逻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天抗斤,我揣著相機(jī)與錄音囚企,去河邊找鬼。 笑死瑞眼,一個胖子當(dāng)著我的面吹牛龙宏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播负拟,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼烦衣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了掩浙?” 一聲冷哼從身側(cè)響起花吟,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎厨姚,沒想到半個月后衅澈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡谬墙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年今布,在試婚紗的時候發(fā)現(xiàn)自己被綠了经备。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡部默,死狀恐怖侵蒙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情傅蹂,我是刑警寧澤纷闺,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站份蝴,受9級特大地震影響犁功,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜婚夫,卻給世界環(huán)境...
    茶點故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一浸卦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧案糙,春花似錦限嫌、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至匣椰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間端礼,已是汗流浹背禽笑。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留蛤奥,地道東北人佳镜。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像凡桥,于是被迫代替她去往敵國和親蟀伸。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,930評論 2 361

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