【问题标题】:How to load an AnimatorSet with animations defined in xml如何使用 xml 中定义的动画加载 AnimatorSet
【发布时间】:2016-12-14 20:06:56
【问题描述】:

我正在尝试连续播放多个动画,一个接一个。这些动画要么是简单的变换(将图像视图移动一定距离),要么是更复杂的帧动画。

在研究这个问题时,AnimatorSet 一开始似乎是一个很好的解决方案。它允许您制作要播放的动画队列,然后按特定顺序执行它们。

    // defining an example animation
    Animator anim = ObjectAnimator.ofFloat(v, "alpha", 0f)
            .setDuration(100);
    AnimatorSet set = new AnimatorSet();

    // dictating the order in which the animations will be played
    set.playSequentially(anim, skewAnim, wobbleAnim);

    set.start();

问题在于,据我所知,您无法从 xml 文件中定义的动画中定义要放入 AnimatorSet 的 Animator 对象。

另外,AnimationSet(注意名字不同)只允许你同时播放多个动画,所以这个类不适用于我的目的。

有人知道在您的项目中从 xml 对象定义 Animator 对象的方法吗?或者,是否有另一种方法可以完全播放一系列动画?

更新:

    AnimatorSet set = new AnimatorSet();
    Animator anim1 = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.sample);
    anim1.setTarget(myView1);
    Animator anim2 = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.sample);
    anim2.setTarget(myView2);

    anim1.setDuration(3000);
    anim2.setDuration(3000);

    anim1.setInterpolator(new CycleInterpolator(5));
    anim2.setInterpolator(new CycleInterpolator(5));

    set.playSequentially(anim1,anim2);
    set.start();

【问题讨论】:

    标签: android animation


    【解决方案1】:

    要将动画从 xml 加载到 AnimatorSet,您的 xml 应该包含 AnimatorObjects 而不是动画。然后您可以使用 AnimatorInflater 将这些 animatorObjects 加载到您的 AnimatorSet 中,如下所示:

    AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), R.animator.sample);
    

    xml 想要这样的东西:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:ordering="sequentially"
        >
    
      <objectAnimator
          android:propertyName="alpha"
          android:duration="500"
          android:valueTo="1f"
          />
      <objectAnimator
        android:propertyName="y"
        android:duration="500"
        android:valueTo="300"
        android:valueType="intType"/>
    </set>
    

    有关AnimatorInflater的更多详细信息,请参阅此链接

    要在不同视图上播放连续动画,您可以使用 AnimationListeners。

    anim1.addListener(new Animator.AnimatorListener() {
                                @Override
                                public void onAnimationStart(Animator animation) {
    
                                }
    
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                 // start anim2 from here
                                }
    
                                @Override
                                public void onAnimationCancel(Animator animation) {
    
                                }
    
                                @Override
                                public void onAnimationRepeat(Animator animation) {
    
                                }});
    

    【讨论】:

    • 谢谢拉文德。你知道为什么我的动画师没有按预期播放动画吗? (见上面的编辑)
    • 如果您将多个动画对象提供到单个 xml 中并使用它来创建 AnimatorSet,那么这些动画将根据 xml 文件中的特定顺序播放。但是从上面的更新我可以看到你不想在不同的视图上播放顺序动画,因为你需要动画监听器,检查我的答案
    猜你喜欢
    • 2015-07-06
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多