在Android的XML动画配置中,animation set标签用于组合多个动画,shareInterpolator属性可以控制集合内的所有子动画是否使用同一个插值器,合理配置该属性能够简化动画逻辑,统一动画的速度变化效果。

shareInterpolator属性基础说明
shareInterpolator是<set>标签的可选属性,取值为布尔类型,默认值为true。当设置为true时,集合内所有子动画都会使用<set>标签上配置的插值器;当设置为false时,每个子动画需要使用自己的插值器配置,如果子动画没有单独配置,会使用系统默认的插值器。
属性取值对照
| 取值 | 含义 | 适用场景 |
|---|---|---|
| true | 集合内所有动画共享同一个插值器 | 多个动画需要相同速度变化规律的场景 |
| false | 每个动画使用各自的插值器 | 不同动画需要不同速度变化规律的场景 |
XML配置示例
共享插值器配置示例
以下配置中,<set>标签设置shareInterpolator为true,同时配置了加速插值器,内部的平移动画和缩放动画都会使用这个插值器。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:interpolator="@android:anim/accelerate_interpolator">
<!-- 平移动画 -->
<translate
android:duration="1000"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="50%"
android:toYDelta="50%" />
<!-- 缩放动画 -->
<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.5"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%" />
</set>
不共享插值器配置示例
以下配置中,shareInterpolator设置为false,<set>标签上的插值器不会生效,平移动画使用加速插值器,缩放动画使用减速插值器。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 平移动画使用加速插值器 -->
<translate
android:duration="1000"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="50%"
android:toYDelta="50%"
android:interpolator="@android:anim/accelerate_interpolator" />
<!-- 缩放动画使用减速插值器 -->
<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.5"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator" />
</set>
代码中加载动画验证
可以通过代码加载上述XML动画,应用到View上观察效果差异,以下是加载动画的示例代码:
// 加载共享插值器的动画配置 Animation sharedAnim = AnimationUtils.loadAnimation(this, R.anim.shared_interpolator_anim); // 加载不共享插值器的动画配置 Animation separateAnim = AnimationUtils.loadAnimation(this, R.anim.separate_interpolator_anim); // 应用到目标View View targetView = findViewById(R.id.target_view); targetView.startAnimation(sharedAnim);
常见注意事项
- 如果shareInterpolator设置为true,但是没有给<set>标签配置interpolator属性,集合内所有动画会使用系统默认的插值器。
- 子动画单独配置的interpolator属性,只有在shareInterpolator为false时才会生效,为true时会被集合的插值器覆盖。
- 嵌套的<set>标签也会遵循父集合的shareInterpolator配置,除非嵌套集合单独设置了自己的shareInterpolator属性。
Androidanimation_setshareInterpolatorXML配置插值器修改时间:2026-07-16 21:12:12