动画 (TranslateAnimation) 中的延迟

16

有没有办法让 Animation 暂停半秒钟?

我正在尝试使用 TranslateAnimation API 创建无限动画。因此,我将 RepeatCount 设置为 Infinite。我还注意到有一个 setStartOffset(...) 方法,可以覆盖当我想要延迟启动动画时的情况。但是,我找不到在每次“重新开始”之前设置延迟的方法。由于动画将无限次发生,因此每次动画重新开始时都需要添加延迟。

有什么想法吗?

谢谢!


也许可以将其放入一个线程中,启动动画,休眠一定时间,然后重新开始动画,而不是使用无限循环? - Cruceo
3个回答

12

以下是一个示例:

首先是我们想要动画化的图片和布局(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>

接下来是动画。它位于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>

最后但并非最不重要的是 - 活动。它开始了动画:

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

9
为了在每次重启之间实现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);
        }

    });

0

myanimation.setStartDelay(int);


你从哪里找到那个方法的? - hector6872
1
@h_rules 他可能是指 setStartOffset(long) - Giulio Piancastelli
2
它必须在视图上调用。view.animate().setDuration(1000).setStartDelay(500); - Yurets

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接