代码之家  ›  专栏  ›  技术社区  ›  amithgc

安卓动画一个接一个

  •  19
  • amithgc  · 技术社区  · 14 年前

    我在一个文本视图上有两个translateAnimations,我希望它们一个接一个地执行。但是,通过使用下面的代码,只执行第二个代码。

    我怎么解决这个问题?

    TranslateAnimation animation = new TranslateAnimation(
        Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
        Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
    animation.setDuration(200);
    wave.startAnimation(animation);
    
    TranslateAnimation animation1 = new TranslateAnimation(
        Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
        Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
    animation1.setDuration(200);
    wave.startAnimation(animation1);
    
    6 回复  |  直到 7 年前
        1
  •  32
  •   pgsandstrom    12 年前

    编辑: 安迪·布茨下面的答案是我最好的答案。


    只需将第一个设置为这样,动画完成后将启动另一个:

    animation.setAnimationListener(new AnimationListener() {
    
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                wave.startAnimation(animation1);
    
            }
        });
    

    编辑:仅使用当前代码执行第二个动画的原因是,它会覆盖第一个动画的播放(两个动画都实际播放,但您只能看到最新的动画)。如果你真的像我写的那样,它们将按顺序播放而不是并行播放。

        2
  •  52
  •   andy boot    13 年前

    将它们与 Animation Set

    AnimationSet as = new AnimationSet(true)
    TranslateAnimation animation = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f);
    animation.setDuration(200);
    as.addAnimation(animation);
    
    TranslateAnimation animation1 = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
    Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f);
    animation1.setDuration(200);
    animation1.setStartOffset(200);
    as.addAnimation(animation1);
    
    wave.startAnimation(as);
    
        3
  •  10
  •   Khaled AbuShqear    10 年前

    此外,还可以使用XML本身 android:startOffset 属性,有一个示例:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale
            android:duration="300"
            android:fromXScale="0%"
            android:fromYScale="0%"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toXScale="100%"
            android:toYScale="100%" />
        <alpha
            android:duration="300"
            android:fromAlpha="0"
            android:toAlpha=".5" />
        <alpha
            android:duration="300"
            android:fromAlpha=".5"
            android:startOffset="300"
            android:toAlpha="1" />
    
    </set>
    
        4
  •  6
  •   Denis Kutlubaev    11 年前

    还有一种方法可以实现这一目标,当您需要一个接一个地为许多视图设置动画时,这种方法非常有用。你可以使用 setStartOffset 方法设置动画开始前的延迟。因此,如果知道第一个动画结束需要多少时间,可以将其设置为第二个动画的延迟。这是一个例子,我在其中设置了六个动画 ImageButtons 和六 TextViews 在它们下面一个接一个:

    public void animateButtons() {
        // An array of buttons
        int[] imageButtonIds = {R.id.searchButton, R.id.favoriteButton, R.id.responseButton, R.id.articleButton, R.id.resumeButton, R.id.subscribeButton};
        // Array of textViews
        int[] textViewIds = {R.id.searchTextView, R.id.favoriteTextView, R.id.responseTextView, R.id.articleTextView, R.id.resumeTextView, R.id.subscribeTextView};
    
        int i = 1;
    
        for (int viewId : imageButtonIds) {
    
            ImageButton imageButton = (ImageButton) findViewById(viewId);
            // Animation from a file fade.xml in folder res/anim
            Animation fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade);
            // Delay for each animation is 100 ms bigger than for previous one
            fadeAnimation.setStartOffset(i * 100);
            imageButton.startAnimation(fadeAnimation);
    
            // The same animation is for textViews
            int textViewId = textViewIds[i-1];
            TextView textView = (TextView) findViewById(textViewId);
            textView.startAnimation(fadeAnimation);
    
            i ++;
        }
    }
    

    在我的 res/anim 文件夹我有一个文件,叫做 fade.xml 包括以下内容:

    <?xml version="1.0" encoding="utf-8"?>
    
    <!--  Fade animation with 500 ms duration -->
    
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_decelerate_interpolator"
           android:fromAlpha="0.0" android:toAlpha="1.0"
           android:duration="500" />
    
        5
  •  1
  •   Mete    7 年前

    创建动画数组并使用创建动画集的方法。

        Animation[] animations = { 
                getScaleAnimation(0.4f, 1.3f, 2000), 
                getScaleAnimation(1.3f, 1.0f, 500), 
                getScaleAnimation(0.4f, 1.3f, 1000),
                getScaleAnimation(1.3f, 1.0f, 3000), 
                getScaleAnimation(0.4f, 1.3f, 500), 
                getScaleAnimation(1.3f, 1.0f, 1700), 
                getScaleAnimation(0.4f, 1.3f, 2100),
                getScaleAnimation(1.3f, 1.0f, 3400)  
        };
        AnimationSet animationSet = addAnimationAr(animations);
        view.startAnimation(animationSet);
    

    方法:

    public static AnimationSet addAnimationAr(Animation[] animations) {
        AnimationSet animationSet = new AnimationSet(false);
        long totalAnimationDuration = 0;
    
        for (int i = 0; i < animations.length; i++) {
            Animation a = animations[i];
            a.setStartOffset(totalAnimationDuration);
            totalAnimationDuration += a.getDuration();
            animationSet.addAnimation(a);
        }
    
        return animationSet;
    }
    
        6
  •  0
  •   ρяσѕρєя K    9 年前

    如果您使用代码,可以调用

    Animation.setStartOffset() 
    

    延迟第二个动画。

    如果使用XML,则可以 android:ordering="sequentially" 属性使两个动画按顺序执行。