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

相关推荐
studyForMokey3 小时前
kotlin 函数类型接口lambda写法
android·开发语言·kotlin
小超嵌入式笔记3 小时前
[教程]Gitee保姆级图文使用教程
git·gitee
血战灬狂龙3 小时前
pom.xml文件加载后没有变成maven图标
xml·java·maven
梁同学与Android6 小时前
Android --- 新电脑安装Android Studio 使用 Android 内置模拟器电脑直接卡死,鼠标和键盘都操作不了
android·ide·android studio
山雨楼8 小时前
ExoPlayer架构详解与源码分析(14)——ProgressiveMediaPeriod
android·架构·音视频·源码·exoplayer·media3
IsaacBan10 小时前
XJBX-6-Android启动App进程
android
DoubleYellowIce10 小时前
Android Studio阅读frameworks源码的正确姿势
android·android studio
分享者花花10 小时前
最佳 iPhone 解锁软件工具,可免费下载用于电脑操作的
android·windows·macos·ios·pdf·word·iphone
菜鸟得菜15 小时前
MAVEN中settings.xml文件中,<mirrors> 元素怎么写?
xml·java·maven
小菜琳15 小时前
Android显式启动activity和隐式启动activity分别都是怎么启动?请举例说明二者使用时的注意事项。
android