Android 动画中的延迟 (TranslateAnimation)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11268033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:36:14  来源:igfitidea点击:

Delays within the Animation (TranslateAnimation)

androidandroid-animationtranslate-animation

提问by ymotov

Is there a way to have the Animationpause for a half a second?

有没有办法让Animation停顿半秒?

I am trying to make an infinite animation using the TranslateAnimationAPI. So, I use the RepeatCountas Infinite. I also noticed that there's a setStartOffset(...)method that covers the case when I'd like to have a delay in starting the animation. However, I can't find a way to have a delay before each 'restart'. Since animation is gonna happen infinite amount of times, every time the animation restarts I need to put a delay in.

我正在尝试使用TranslateAnimationAPI制作无限动画。所以,我使用RepeatCountas Infinite。我还注意到有一种setStartOffset(...)方法可以涵盖我想要延迟启动动画的情况。但是,我找不到在每次“重新启动”之前延迟的方法。由于动画会发生无限次,每次动画重新启动时我都需要延迟。

Any ideas?

有任何想法吗?

Thanks!!

谢谢!!

采纳答案by Paul Spiesberger

Here is an example:

下面是一个例子:

First the layout (main.xml) with an image we would like to animate:

首先是带有我们想要动画的图像的布局 (main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

Next one is the animation. Placed in res/anim and is called anim_img.xml. The file contains the translation animation with android:startOffset="500" (in millisec). This will set the offset, which is used every time animation starts:

下一个是动画。放置在 res/anim 中,名为 anim_img.xml。该文件包含带有 android:startOffset="500"(以毫秒为单位)的翻译动画。这将设置每次动画开始时使用的偏移量:

<?xml version="1.0" encoding="utf-8"?>
<set>

    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="100%"
        android:zAdjustment="top" 
        android:repeatCount="infinite"
        android:startOffset="500"/>

</set>

And last but not least - the activity. Which starts the animation:

最后但并非最不重要的 - 活动。启动动画:

public class StackOverflowActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView iv_icon = (ImageView) findViewById(R.id.imageView1);

        Animation a = AnimationUtils.loadAnimation(this, R.anim.anim_img);
        a.setFillAfter(true);
        a.reset();

        iv_icon.startAnimation(a);
    }
}

回答by Paola G

To achieve a pause of x milliseconds between each restart:

要在每次重新启动之间实现 x 毫秒的暂停:

myAnimation.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationStart(Animation arg0) {
        }
        @Override
        public void onAnimationEnd(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            myAnimation.setStartOffset(x);
        }

    });

回答by Drx

myanimation.setStartDelay(int);

myanimation.setStartDelay(int);