[英]How to animate background color change on relative layouts views when pressing a ToggleButton in Android?
[英]Android animate layouts and views
我有一個ConstraintLayout
,底部有一個導航,一個片段容器占據了屏幕的其余部分。 我想要的是以編程方式添加/刪除導航和帶有動畫的片段容器之間的視圖。
我有這個代碼來添加和動畫新視圖:
//Adding the view to the main layout
ConstraintLayout layout = findViewById(R.id.ConstraintLayout);
getLayoutInflater().inflate(R.layout.newView, layout);
//Start animation immediately with a library
YoYo.with(Techniques.SlideInUp).duration(800).playOn(findViewById(R.id.newView));
現在上面的代碼對我來說並不可靠。 我想找到更好的方法,但現在它有效。
添加新視圖后,我將片段容器的約束從導航頂部更改為新視圖頂部。 片段容器的大小設置為 match_constrainst,因此通過更改約束,它現在會變小一點。
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout);
constraintSet.connect(R.id.fragment_container,ConstraintSet.BOTTOM,R.id.newViewWrapper,ConstraintSet.TOP,0);
constraintSet.applyTo(layout);
我沒有設法做的是動畫片段容器的大小變化。 總的來說,我從未在 android 中使用過動畫。
在android中是否有更好或最佳實踐的動畫視圖和布局方法,或者是否有可以做到這一點的庫?
如果您使用Constraintlayout
,則可以將TransitionManager
用於布局動畫。 基本設置是這樣的:
TransitionManager.beginDelayedTransition(layout);
例如,一旦您更改視圖/約束的大小,它將自動為布局設置動畫。
要更改約束,您可以使用ConstraintSet
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.