實(shí)現(xiàn) Android 卡片切換效果有多種方法,以下是其中一種常見的實(shí)現(xiàn)方式:
以下是一個(gè)簡(jiǎn)單的示例代碼,實(shí)現(xiàn)了兩個(gè)卡片之間的切換效果:
// 在 XML 布局文件中定義卡片容器和卡片視圖
<LinearLayout
android:id="@+id/cardContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/card1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:visibility="visible">
<!-- 卡片1的內(nèi)容 -->
</LinearLayout>
<LinearLayout
android:id="@+id/card2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:visibility="gone">
<!-- 卡片2的內(nèi)容 -->
</LinearLayout>
</LinearLayout>
// 在 Activity 或 Fragment 中實(shí)現(xiàn)卡片切換
private LinearLayout cardContainer;
private LinearLayout card1;
private LinearLayout card2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cardContainer = findViewById(R.id.cardContainer);
card1 = findViewById(R.id.card1);
card2 = findViewById(R.id.card2);
}
private void switchCard() {
// 切換卡片的可見性
if (card1.getVisibility() == View.VISIBLE) {
card1.setVisibility(View.GONE);
card2.setVisibility(View.VISIBLE);
} else {
card1.setVisibility(View.VISIBLE);
card2.setVisibility(View.GONE);
}
// 定義卡片切換動(dòng)畫效果
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(cardContainer, "scaleX", 0.8f, 1.0f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(cardContainer, "scaleY", 0.8f, 1.0f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(500);
animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
animatorSet.start();
}
在需要切換卡片時(shí),調(diào)用 switchCard()
方法即可啟動(dòng)切換動(dòng)畫。