要實(shí)現(xiàn)Android MaterialCardView的動(dòng)畫效果,可以使用屬性動(dòng)畫或布局動(dòng)畫來(lái)實(shí)現(xiàn)。以下是一個(gè)簡(jiǎn)單的示例:
MaterialCardView cardView = findViewById(R.id.cardView);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(cardView, "scaleX", 0.5f, 1.0f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(cardView, "scaleY", 0.5f, 1.0f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(scaleX, scaleY);
animatorSet.setDuration(1000);
animatorSet.start();
在res/anim文件夾下創(chuàng)建一個(gè)scale.xml文件,并添加以下內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="1000"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:interpolator/accelerate_decelerate"/>
</set>
然后在代碼中使用布局動(dòng)畫來(lái)實(shí)現(xiàn)動(dòng)畫效果:
MaterialCardView cardView = findViewById(R.id.cardView);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
cardView.startAnimation(animation);
這樣就可以實(shí)現(xiàn)MaterialCardView的動(dòng)畫效果了。您可以根據(jù)需要調(diào)整動(dòng)畫的屬性和持續(xù)時(shí)間來(lái)達(dá)到更好的效果。