自定义控件之动画篇(六)——联合动画的代码及xml实现

在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还是代码,都可以根据具体需求灵活选择。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值