繁体   English   中英

如何将 android 中的 animation 链接到相同的视图?

[英]How to chain animation in android to the same view?

我得到了一些文本视图,我想制作 MSN 的嗡嗡声效果。

我的计划是:

  • 看风景,让我们说左边 10dip,
  • 让它回到起点 position
  • 在那之后采取它10dip up
  • 然后回来
  • 往后退
  • 离开……等等。

我的观点是,我想将一些动作序列设置为一个视图,并且需要一个接一个地执行。

我怎样才能做到这一点?

您可能是指AnimatorSet (不是AnimationSet )。 如文档中所述:

这个 class 按指定顺序播放一组Animator对象。 动画可以设置为一起播放、按顺序播放或在指定延迟后播放。

向 AnimatorSet 添加动画有两种不同的方法:可以playTogether()playSequentially()方法来一次添加一组动画,或者play(Animator)可以与Builder class 一个一个添加动画。

-100pxview移动 -100 像素700ms ,然后在300ms内消失:

final View view = findViewById(R.id.my_view);

final Animator translationAnimator = ObjectAnimator
        .ofFloat(view, View.TRANSLATION_Y, 0f, -100f)
        .setDuration(700);

final Animator alphaAnimator = ObjectAnimator
        .ofFloat(view, View.ALPHA, 1f, 0f)
        .setDuration(300);

final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(
        translationAnimator,
        alphaAnimator
);
animatorSet.start()

我有一个 sdk 15 兼容 class 的开始,可用于构建复杂的 animation 链希望它对某人有所帮助。 您应该能够按照设计模式添加自己的方法。 如果你这样做,请在这里评论他们,我会更新答案,干杯!

package com.stuartclark45.magicmatt.util;

import java.util.LinkedList;
import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;

    /**
     * Used to build complex animations for a view. Example usage bellow makes view move out to the
     * right whilst rotating 45 degrees, then move out to the left.
     *
     * {@code
     *  int rotateDuration = 200;
     *  int rotation = 45;
     *  new AnimationBuilder(view)
     *    .translationX(100, rotateDuration)
     *    .rotateTo(rotation, rotateDuration)
     *    .then()
     *    .translationX(-200, rotateDuration)
     *    .start();
     * }
     *
     * @author Stuart Clark
     */

    public class AnimationBuilder {

      private View view;
      private List<Animator> setsList;
      private List<Animator> buildingList;

      public AnimationBuilder(View view) {
        this.view = view;
        this.setsList = new LinkedList<>();
        this.buildingList = new LinkedList<>();
      }

      public AnimationBuilder rotateTo(float deg, long duration) {
        buildingList.add(ObjectAnimator.ofFloat(view, "rotation", deg).setDuration(duration));
        return this;
      }

      public AnimationBuilder translationX(int deltaX, long duration) {
        buildingList.add(ObjectAnimator.ofFloat(view, "translationX", deltaX).setDuration(duration));
        return this;
      }

      public AnimationBuilder translationY(int deltaX, long duration) {
        buildingList.add(ObjectAnimator.ofFloat(view, "translationY", deltaX).setDuration(duration));
        return this;
      }

      public AnimationBuilder then() {
        createAniSet();
        // Reset the building list
        buildingList = new LinkedList<>();
        return this;
      }

      public void start() {
        createAniSet();
        AnimatorSet metaSet = new AnimatorSet();
        metaSet.playSequentially(setsList);
        metaSet.start();
      }

      private void createAniSet() {
        AnimatorSet aniSet = new AnimatorSet();
        aniSet.playTogether(buildingList);
        setsList.add(aniSet);
      }


    }

使用AnimationSet

AnimationSet set = new AnimationSet(true);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);

animation = new TranslateAnimation(
    Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
    Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(500);
set.addAnimation(animation);

view.startAnimation( set );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM