最近在嘗試實作CardView上的動畫, 趁記憶還很新, 來做個紀錄
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/cl_background"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="323dp"
android:layout_marginRight="323dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button">
<LinearLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="29sp" />
<TextView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="Title"
android:textSize="29sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
大家可以看到主要是CardView裏面包含了 基本的Layout加上 TextView
button.setOnClickListener {
val autoTransition = ChangeBounds()
autoTransition.duration = 200
TransitionManager.beginDelayedTransition(cl_background, autoTransition)
if (mIsExpand) {
mIsExpand = false
view.visibility = View.GONE
} else {
mIsExpand = true
view.visibility = View.VISIBLE
}
}
可以看到再使用TransitonManager.beginDelayedTransition cl_background 目前使用上的心得要擺的Layout是最外層的Layout 如果是擺CardView是不會有任何效果的
而且控制的方式是純粹呼叫Visiblity Visible and Gone 這樣子在控制的時候就會產生動畫幫你做出來使用上還算簡單