Android利用ConstraintLayout實(shí)現(xiàn)漂亮的動(dòng)畫(huà)詳解
前言
最近ConstrainLayout是Android中比較火的一個(gè)東西。ConstrainLayout可以使View層級(jí)扁平化,提升性能,支持任意的邊框,其目的就是修復(fù)之前l(fā)ayout的一些短板。其實(shí)ConstrainLayout還有一個(gè)大多數(shù)人沒(méi)有注意到的特性:可以利用Constrainlayout快速構(gòu)建漂亮的動(dòng)畫(huà)效果。
方法
我這里假設(shè)已經(jīng)你已經(jīng)掌握了Constrainlayout基本知識(shí)(比如:app:layout_constraintLeft_toLeftOf等)。Constrainlayout可以通過(guò)TransitionManager 在兩組constraints之間執(zhí)行動(dòng)畫(huà)(需要API>19或者使用support library),以下是一個(gè)demo。
Simple demo
我們先寫(xiě)一個(gè)xml布局:
<!-- activity_main.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout>
到目前為止,這只是一個(gè)普通的xml布局,我們?cè)俣x另一個(gè)布局:
<!-- activity_main_alt.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" **app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"** ... /> <Button ... /> </android.support.constraint.ConstraintLayout>
這兩個(gè)布局只有ImageView的高度不同,為了執(zhí)行動(dòng)畫(huà),只需要在Activity中寫(xiě)幾行代碼即可:
override fun onCreate(savedInstanceState: Bundle?) { ... val constraintSet1 = ConstraintSet() constraintSet1.clone(constraintLayout) val constraintSet2 = ConstraintSet() constraintSet2.clone(this, R.layout.activity_main_alt) var changed = false findViewById(R.id.button).setOnClickListener { TransitionManager.beginDelayedTransition(constraintLayout) val constraint = if (changed) constraintSet1 else constraintSet2 constraint.applyTo(constraintLayout) changed = !changed } }
代碼使用Kotlin寫(xiě)的,即使沒(méi)有學(xué)過(guò),基本也沒(méi)有什么障礙,不過(guò)還是很有必要學(xué)習(xí)一下的。
代碼中我們使用TransitionManager在Constrainlayout中啟動(dòng)了一個(gè)延時(shí)動(dòng)畫(huà),TransitionManager在交換兩種布局時(shí)會(huì)自動(dòng)使用動(dòng)畫(huà)。
重復(fù)的xml Layout
這種方式使用了兩個(gè)xml布局,是否重復(fù)了呢,沒(méi)有人喜歡重復(fù)的代碼。
其實(shí)沒(méi)有你想的那么糟糕,如果為了動(dòng)畫(huà)的目的定義多余的xml,可以省略所有的非布局屬性(如textSize等屬性)。Constrainlayout會(huì)自動(dòng)捕獲所有l(wèi)ayout的基本約束屬性并拋棄其中的一些。
如果你還是想避免重復(fù)的代碼,還可以在代碼中動(dòng)態(tài)修改約束屬性:
override fun onCreate(savedInstanceState: Bundle?) { ... val constraintSet1 = ConstraintSet() constraintSet1.clone(constraintLayout) val constraintSet2 = ConstraintSet() constraintSet2.clone(constraintLayout) constraintSet2.centerVertically(R.id.image, 0) var changed = false findViewById(R.id.button).setOnClickListener { TransitionManager.beginDelayedTransition(constraintLayout) val constraint = if (changed) constraintSet1 else constraintSet2 constraint.applyTo(constraintLayout) changed = !changed } }
使用transition 框架也可以實(shí)現(xiàn)這些動(dòng)畫(huà)
當(dāng)然可以這樣,我們可以通過(guò)使用transition框架或者使用屬性設(shè)置也可以實(shí)現(xiàn)動(dòng)畫(huà)。然而,當(dāng)需要的動(dòng)畫(huà)可通過(guò)使用特定的約束來(lái)實(shí)現(xiàn)時(shí),ConstrainLayout的方法就很有效,否則就需要大量的代碼來(lái)實(shí)現(xiàn)動(dòng)畫(huà)效果。
另一個(gè)使用場(chǎng)景是當(dāng)很多元素需要?jiǎng)有r(shí),看一個(gè)例子:
使用ConstrainLayout可以實(shí)現(xiàn)以上的效果,通過(guò)指定不同的xml,動(dòng)畫(huà)就會(huì)自動(dòng)執(zhí)行。
注意事項(xiàng)
1. 啟動(dòng)動(dòng)畫(huà)的方法:
TransitionManager.beginDelayedTransition(constraintLayout)
2. 自定義動(dòng)畫(huà)
還可以自定義Transition:
val transition = AutoTransition() transition.duration = 1000 TransitionManager.beginDelayedTransition( constraintLayout, transition)
3. 嵌套問(wèn)題
ConstraintLayout只可以對(duì)其直接子View執(zhí)行動(dòng)畫(huà),這就意味著它不能很好地處理嵌套的ViewGroup。在以上的例子中,CardView中的TextView還需要手動(dòng)處理動(dòng)畫(huà),也許可以通過(guò)嵌套ConstrainLayout來(lái)實(shí)現(xiàn),但是我并沒(méi)有進(jìn)行實(shí)驗(yàn)。
4. 非布局屬性
ConstraintLayout只支持約束布局的動(dòng)畫(huà),不支持其他屬性(如坐標(biāo)修改,文字修改等)。
5. 其他
如果動(dòng)態(tài)修改ConstraintLayout中元素的基本布局屬性(比如使用translationY),動(dòng)畫(huà)后并不會(huì)同步這個(gè)變更,也就是說(shuō)動(dòng)畫(huà)執(zhí)行后,之前修改的屬性將會(huì)復(fù)原。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android開(kāi)發(fā)之5.0activity跳轉(zhuǎn)時(shí)共享元素的使用方法
下面小編就為大家分享一篇Android開(kāi)發(fā)之5.0activity跳轉(zhuǎn)時(shí)共享元素的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android簡(jiǎn)單實(shí)現(xiàn)自定義彈框(PopupWindow)
本文主要介紹了Android利用PopupWindow實(shí)現(xiàn)自定義彈框的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04Java和Android的LRU緩存及實(shí)現(xiàn)原理
本文主要介紹 Java和Android的LRU緩存及實(shí)現(xiàn)原理,這里整理了詳細(xì)的資料,有興趣的小伙伴可以參考下便于學(xué)習(xí)理解2016-08-08Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效,實(shí)現(xiàn)精彩的阻尼效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Flutter上線項(xiàng)目實(shí)戰(zhàn)記錄之路由篇
這篇文章主要給大家介紹了關(guān)于Flutter上線項(xiàng)目實(shí)戰(zhàn)記錄之路由篇的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Android答題APP的設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android答題APP的設(shè)計(jì)與實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Android實(shí)戰(zhàn)教程第五篇之一鍵鎖屏應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第五篇之一鍵鎖屏應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android自定義軟鍵盤(pán)的設(shè)計(jì)與實(shí)現(xiàn)代碼
本篇文章主要介紹了 Android自定義軟鍵盤(pán)的設(shè)計(jì)與實(shí)現(xiàn)代碼,有需要的可以了解一下。2016-11-11