如何将android中的动画链接到同一个视图?

发布于 2024-11-14 10:27:17 字数 238 浏览 3 评论 0原文

我得到了一些文本浏览量,我想制作MSN的嗡嗡声效果。

我的计划是:

  • 观察视图,假设向左倾斜 10 次,然后
  • 将其带回起始位置,
  • 然后向上倾斜 10 次,
  • 然后再
  • 向左向下倾斜
  • ...等等。

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

我怎样才能做到这一点?

I got some text views and I want to make the buzz effect of MSN.

My plan is:

  • take the view, let say 10dip to the left,
  • take it back to its start position
  • after that take it 10dip up
  • then back
  • down back
  • left... and so on.

My point is, I have some sequence of movements that I want to set to one view and that needs to execute one after another.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亚希 2024-11-21 10:27:17

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

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

向 AnimatorSet 添加动画有两种不同的方法:可以调用 playTogether()playSequentially() 方法来添加一组动画一次,或者play(Animator)可以与Builder类中的方法结合使用来逐个添加动画。

动画将 view 移动 -100px,持续 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()

You probably mean AnimatorSet (not AnimationSet). As written in documentation:

This class plays a set of Animator objects in the specified order. Animations can be set up to play together, in sequence, or after a specified delay.

There are two different approaches to adding animations to a AnimatorSet: either the playTogether() or playSequentially() methods can be called to add a set of animations all at once, or the play(Animator) can be used in conjunction with methods in the Builder class to add animations one by one.

Animation which moves view by -100px for 700ms and then disappears during 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()
滿滿的愛 2024-11-21 10:27:17

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

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);
      }


    }

I have the beginnings of an sdk 15 compatible class that can be used to build complex animation chains hope it helps someone. You should be able to follow the design pattern to add your own methods. If you do please comment them here and I will update the answer, Cheers!

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);
      }


    }
长亭外,古道边 2024-11-21 10:27:17

使用 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 );

Use an 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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文