Android的多AnimatorSet复位 [英] Android Multiple AnimatorSet reset

查看:2918
本文介绍了Android的多AnimatorSet复位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在申请多个动画使用AnimatorSet和ValueAnimator景色。

Hi I am applying multiple animations to a view using AnimatorSet and ValueAnimator.

当用户触摸视图,连续动画应用于视图。我这样做有一个放大效应,当用户在视图上移动手指。

When the user touches the view, successive animations are applied to the view. I do this to have a "zooming effect" when the user is moving his fingers on the view.

该视图是一个自定义的画廊。

The view is a custom gallery.

下面是code动画:

private void createAnim(final View v) {
    animating = true;
    if (D)
        Log.i(TAG, "sarting animation");
    try {
        v.clearAnimation();
        v.getAnimation().reset();
    } catch (Exception e) {

    }

    set = new AnimatorSet();

    set.addListener(new AnimatorListener() {

        @Override
        public void onAnimationStart(Animator animation) {


        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (D)
                Log.i(TAG, "Animation ended");
            animating = false;
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            animating = false;
            if (D)
                Log.i(TAG, "Animation cancelled");

        }
    });     

    set.setDuration(100);

    ValueAnimator v1 = ObjectAnimator.ofFloat(v, "scaleX", mScaleFactor);
    ValueAnimator v2 = ObjectAnimator.ofFloat(v, "scaleY", mScaleFactor);

    animationList.add(v1);
    animationList.add(v2);
    set.playTogether(v1, v2);

set.start()
}

我有一个onTouchListener和行动的举动我先后调用此方法增加或减少mScaleFactor。

I have an onTouchListener and on action move I successively call this method increasing or decreasing the mScaleFactor.

当用户释放手指,我想的看法回去previous的状态,我的意思是抹去应用,就好像从来没有应用查看所有动画。问题是,你可以很容易地做到这一点对一个动画但是对于很多喜欢,我只是找不到正确的方式。以下是我曾尝试:

When the user releases the finger I would like the view to go back to previous state, I mean to erase all animations applied as if they were never applied to view. The problem is you can easily do that for one animation but for many like that I just can't find the right way. Here is what I have tried:

-Adding动画到一个ArrayList和做对他们每个人animation.reverse()与加入到他们每个人+100延迟等等层出不穷,但最终结果执行一个只是似乎并不完全同之前的动画

-Adding the animations to an ArrayList and doing animation.reverse() on each one of them with a delay of +100 added to each of them so the execute one after another but the end result just doesn't seem exactly the same as before animations.

v.clearAnimation();只取消最后一个动画
 v.getAnimation()重置()。同样仅适用于最后/主动动画。
有先手的观点回来,因为它是所有动画尚未启动之前的方法?
非常感谢

v.clearAnimation(); only cancels the last animation v.getAnimation().reset(); same only works for last/active animation. Is there a method to just get the view back as it was before any animation has been started? Thanks a lot

推荐答案

反转的效果的 AnimatorSet 可能会非常棘手,只是因为没有反向的方法。在这种情况下,最简单的方法,很可能仅仅是另一个 AnimatorSet 完成原始的对立面。这里的问题是你的 AnimatorListener 可能是不同步的。如果你做这样的,你想保持布尔,那么你就必须为有些信号量的实现它。

Reversing the effects of an AnimatorSet can be tricky simply because there is no "reverse" method. The easiest method in this case, would probably simply be another AnimatorSet that does the opposite of the original. The issue here would be is your AnimatorListener could be out of sync. If you do this way and you want to keep the boolean, then you'd have to implement it as somewhat of a semaphore.

public static class AnimatorTracker implements AnimatorListener{
    int counter;

    public AnimatorTracker() {
      counter = 0;
    }

    public boolean isAnimating() {
      return counter == 0;
    }

    @Override
    public void onAnimationStart(Animator animation) {
      counter++;
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    @Override
    public void onAnimationEnd(Animator animation) {
      counter--;
    }

    @Override
    public void onAnimationCancel(Animator animation) {
      // Canceling an animation invokes onAnimationEnd, so nothing needs to be done.
    }
  }

然后,只需让你套只是做同样的方式:

Then just make the sets the same way you just did:

AnimatorTracker tracker = new AnimatorTracker(); // or make it a global reference
AnimatorSet set = new AnimatorSet();
AnimatorSet reverseSet = new AnimatorSet();

ValueAnimator v1 = ObjectAnimator.ofFloat(v, "scaleX", mScaleFactor);
ValueAnimator v2 = ObjectAnimator.ofFloat(v, "scaleY", mScaleFactor);
ValueAnimator reverseV1 = ObjectAnimator.ofFloat(v, "scaleX", 1.0f);
ValueAnimator reverseV2 = ObjectAnimator.ofFloat(v, "scaleY", 1.0f);


set.addListener(tracker);
set.setDuration(100);
set.playTogether(v1, v2);

reverseSet.addListener(tracker);
reverseSet.setDuration(100);
reverseSet.playTogether(reverseV1, reverseV2);

呼叫 set.start()当你想向前动画。呼叫 reverseSet.start()当你想回动画

Call set.start() when you want to animate forward. Call reverseSet.start() when you want to animate back.

这篇关于Android的多AnimatorSet复位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆