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

相关推荐
CANI_PLUS9 小时前
ESP32将DHT11温湿度传感器采集的数据上传到XAMPP的MySQL数据库
android·数据库·mysql
来来走走10 小时前
Flutter SharedPreferences存储数据基本使用
android·flutter
安卓开发者11 小时前
Android模块化架构深度解析:从设计到实践
android·架构
雨白12 小时前
HTTP协议详解(二):深入理解Header与Body
android·http
阿豪元代码12 小时前
深入理解 SurfaceFlinger —— 如何调试 SurfaceFlinger
android
阿豪元代码12 小时前
深入理解 SurfaceFlinger —— 概述
android
CV资深专家14 小时前
Launcher3启动
android
stevenzqzq14 小时前
glide缓存策略和缓存命中
android·缓存·glide
雅雅姐15 小时前
Android 16 的用户和用户组定义
android
没有了遇见15 小时前
Android ConstraintLayout 之ConstraintSet
android