自定义控件之动画篇(六)——联合动画的代码及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还是代码,都可以根据具体需求灵活选择。

相关推荐
jyan_敬言21 分钟前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
程序员老刘41 分钟前
Android 16开发者全解读
android·flutter·客户端
福柯柯2 小时前
Android ContentProvider的使用
android·contenprovider
不想迷路的小男孩2 小时前
Android Studio 中Palette跟Component Tree面板消失怎么恢复正常
android·ide·android studio
餐桌上的王子2 小时前
Android 构建可管理生命周期的应用(一)
android
菠萝加点糖2 小时前
Android Camera2 + OpenGL离屏渲染示例
android·opengl·camera
用户2018792831672 小时前
🌟 童话:四大Context徽章诞生记
android
yzpyzp2 小时前
Android studio在点击运行按钮时执行过程中输出的compileDebugKotlin 这个任务是由gradle执行的吗
android·gradle·android studio
aningxiaoxixi2 小时前
安卓之service
android
TeleostNaCl3 小时前
Android 应用开发 | 一种限制拷贝速率解决因 IO 过高导致系统卡顿的方法
android·经验分享