在Android中,聯合動畫(即組合多種類型的動畫)可以通過編寫Java/Kotlin代碼或XML資源文件來實現。這里我們將分別展示如何通過這兩種方式來實現一個簡單的自定義控件動畫,該動畫將包含平移和縮放效果。
1. XML 資源文件實現
首先,你需要在你的項目中創建一個新的XML動畫資源文件,通常位于 res/anim
目錄下。如果目錄不存在,你需要手動創建。
創建 XML 動畫資源文件
Xml
1<!-- res/anim/custom_animation.xml -->
2<set xmlns:android="http://schemas.android.com/apk/res/android"
3 android:fillAfter="true">
4
5 <translate
6 android:fromXDelta="0%"
7 android:toXDelta="50%"
8 android:fromYDelta="0%"
9 android:toYDelta="50%"
10 android:duration="1000"/>
11
12 <scale
13 android:fromXScale="1.0"
14 android:toXScale="1.5"
15 android:fromYScale="1.0"
16 android:toYScale="1.5"
17 android:pivotX="50%"
18 android:pivotY="50%"
19 android:startOffset="1000"
20 android:duration="1000"/>
21</set>
在這個例子中,我們定義了一個 <set>
動畫,它包含了一個平移動畫(<translate>
)和一個縮放動畫(<scale>
)。平移動畫持續1秒,緊接著是縮放動畫,后者在平移動畫結束后開始,也持續1秒。
在代碼中引用動畫
在你的Activity或Fragment中,你可以這樣引用并播放這個動畫:
Java
1// Java
2Button myButton = findViewById(R.id.my_button);
3Animation animation = AnimationUtils.loadAnimation(this, R.anim.custom_animation);
4myButton.startAnimation(animation);
或者使用Kotlin:
Kotlin
1// Kotlin
2val myButton: Button = findViewById(R.id.my_button)
3val animation: Animation = AnimationUtils.loadAnimation(this, R.anim.custom_animation)
4myButton.startAnimation(animation)
2. Java/Kotlin 代碼實現
你也可以直接在代碼中使用 AnimatorSet
來組合多個 Animator
對象,從而實現更精細的控制。
創建動畫代碼
Java
1// Java
2Button myButton = findViewById(R.id.my_button);
3
4// 創建平移動畫
5ObjectAnimator translateX = ObjectAnimator.ofFloat(myButton, View.TRANSLATION_X, 0, 50);
6ObjectAnimator translateY = ObjectAnimator.ofFloat(myButton, View.TRANSLATION_Y, 0, 50);
7AnimatorSet translateSet = new AnimatorSet();
8translateSet.playTogether(translateX, translateY);
9translateSet.setDuration(1000);
10
11// 創建縮放動畫
12ObjectAnimator scaleX = ObjectAnimator.ofFloat(myButton, View.SCALE_X, 1.0f, 1.5f);
13ObjectAnimator scaleY = ObjectAnimator.ofFloat(myButton, View.SCALE_Y, 1.0f, 1.5f);
14AnimatorSet scaleSet = new AnimatorSet();
15scaleSet.playTogether(scaleX, scaleY);
16scaleSet.setDuration(1000);
17scaleSet.setStartDelay(1000); // 在平移動畫后開始
18
19// 創建組合動畫
20AnimatorSet combinedAnimation = new AnimatorSet();
21combinedAnimation.playSequentially(translateSet, scaleSet);
22combinedAnimation.start();
或者使用Kotlin:
Kotlin
1// Kotlin
2val myButton: Button = findViewById(R.id.my_button)
3
4// 創建平移動畫
5val translateX = ObjectAnimator.ofFloat(myButton, View.TRANSLATION_X, 0f, 50f)
6val translateY = ObjectAnimator.ofFloat(myButton, View.TRANSLATION_Y, 0f, 50f)
7val translateSet = AnimatorSet().apply {
8 playTogether(translateX, translateY)
9 duration = 1000
10}
11
12// 創建縮放動畫
13val scaleX = ObjectAnimator.ofFloat(myButton, View.SCALE_X, 1.0f, 1.5f)
14val scaleY = ObjectAnimator.ofFloat(myButton, View.SCALE_Y, 1.0f, 1.5f)
15val scaleSet = AnimatorSet().apply {
16 playTogether(scaleX, scaleY)
17 duration = 1000
18 startDelay = 1000 // 在平移動畫后開始
19}
20
21// 創建組合動畫
22val combinedAnimation = AnimatorSet().apply {
23 playSequentially(translateSet, scaleSet)
24}
25combinedAnimation.start()
通過以上兩種方法,你可以輕松地在Android中實現聯合動畫,無論是使用XML還是代碼,都可以根據具體需求靈活選擇。