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

相关推荐
回家吃月饼3 小时前
pycharm2018配置gitee操作
pycharm·gitee
雨白4 小时前
Jetpack系列(三):Room数据库——从增删改查到数据库平滑升级
android·android jetpack
花王江不语7 小时前
android studio 配置硬件加速 haxm
android·ide·android studio
江太翁9 小时前
mediapipe流水线分析 三
android·mediapipe
wu_aceo9 小时前
将本地项目提交到Gitee
git·gitee·提交·本地提交·上传git
与火星的孩子对话9 小时前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
tmacfrank10 小时前
Android 网络全栈攻略(四)—— TCPIP 协议族与 HTTPS 协议
android·网络·https
fundroid11 小时前
Kotlin 协程:Channel 与 Flow 深度对比及 Channel 使用指南
android·kotlin·协程
草字11 小时前
cocos 打包安卓
android
DeBuggggggg12 小时前
centos 7.6安装mysql8
android