带有反弹插值器的Android缩放动画

43

我正在使用anim xml进行比例动画,如下所示。 然而动画插值器(interpolator)未生效。 我尝试使用弹跳插值器,但并未生效。

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="900"
        android:fromXScale="1"
        android:fromYScale="0.5"
        android:interpolator="@android:anim/bounce_interpolator"
        android:pivotX="50%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
     </set>

编辑: 实际上我的整个XML文件是:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<scale
    android:duration="600"
    android:fromXScale="1"
    android:fromYScale="0.5"
    android:interpolator="@android:anim/bounce_interpolator"
    android:pivotX="50%"
    android:pivotY="0%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

<alpha
    android:duration="@android:integer/config_longAnimTime"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toAlpha="1.0" />

</set>
4个回答

108

终于找到了解决方案。它对我有用,也可能对其他人有帮助。关键是在动画集中加入android:interpolator标签。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator" >

<scale
    android:duration="600"
    android:fromXScale="1"
    android:fromYScale="0.5"
    android:pivotX="50%"
    android:pivotY="0%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

<alpha
    android:duration="600"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />

</set>

哈哈,如果我知道你有两个动画的话...不管怎样,你的答案很好,接受它可以让其他用户更清楚。 - Snicolas
@Snicolas,你有没有想法为什么在文档中给出的插值器在动画中不起作用?http://developer.android.com/reference/android/view/animation/Animation.html#attr_android:interpolator - Gaurav Vashisth
你能展示一下动画的结果吗?我刚开始学习这个领域的动画,想看一些例子。 - WitaloBenicio
@GauravVashisth:在这个动画中,它是从顶部开始的。我需要的是从底部开始。我尝试了很多次,但都没有成功。 - DKV
您还可以使用自定义插值器来控制弹跳效果,请参考此指南:http://evgenii.com/blog/spring-button-animation-on-android/ - Guy West
显示剩余2条评论

23

如果您想在动画集中为动画使用不同的插值器,请将 shareInterpolator 属性设置为 false,例如:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">

然后在每个动画元素中设置一个插值器。显然,默认情况下 shareInterpolator 被设置为 true。


我被这个问题困扰了几个小时,我不明白为什么我们可以为所有东西使用单独的属性,除了插值器,而原因是默认情况下是真实的,就像这个答案指出的那样。 - Shadow
这应该是被接受的答案。 - mathematics-and-caffeine

2
尝试添加一个持续时间:
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<scale 
  android:fromXScale="1"
  android:fromYScale="0.5"
  android:interpolator="@android:anim/bounce_interpolator"
  android:pivotX="50%"
  android:pivotY="0%"
  android:toXScale="1.0"
  android:toYScale="1.0"
  android:duration="1000" />
</set>

请注意,如果动画集中只包含一个动画,则该动画集将无用。


谢谢,但是持续时间已经存在了 android:duration="900"。我成功让动画工作了,但是没有弹跳效果。 - Gaurav Vashisth
1
你尝试过移除动画集吗? - Snicolas
修改了问题,我也尝试过删除alpha和动画集。 - Gaurav Vashisth

0

Bounce是一种动画效果,其中动画以弹跳的方式结束。为此,请将android:interpolator值设置为@ android:anim / bounce_interpolator。这个反弹可以与任何类型的动画一起使用。以下向下滑动示例使用了反弹效果。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator">

<scale
    android:duration="500"
    android:fromXScale="1.0"
    android:fromYScale="0.0"
    android:toXScale="1.0"
    android:toYScale="1.0" />


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接