在重新启动活动之前延迟动画

时间:2016-11-26 15:47:33

标签: java android animation runnable postdelayed

我有一个GifImageButton视图。我想开始动画,然后重新开始活动。

问题是我希望动画在重新启动活动前持续3秒。

我该怎么做?

这是我的代码:

myGifImageButton.setImageResource(R.drawable.animation);
Intent intent = getIntent();
finish();
if (intent != null) {
    startActivity(intent);
}

正如我所读到的,更好的方法是使用runnable,所以我尝试了这个但是我没有成功:

// start the animation
myGifImageButton.setImageResource(R.drawable.animation);

// delay the animation
mHandler = new Handler();
final Runnable r = new Runnable() {
    void run() {
        handler.postDelayed(this, 3000);
    }
};
handler.postDelayed(r, 3000);

// restart the activity
Intent intent = getIntent();
finish();
if (intent != null) {
    startActivity(intent);
}

那么如何在重新启动活动之前延迟动画呢?

1 个答案:

答案 0 :(得分:1)

Yor runnable不正确 - 你不断重新发布同样没有任何作用的runnable。

而是尝试这样的事情:

// start the animation
myGifImageButton.setImageResource(R.drawable.animation);

// delay the animation
mHandler = new Handler();
final Runnable r = new Runnable() {
    void run() {
       // restart the activity
       Intent intent = getIntent();
       finish();
       if (intent != null) {
           startActivity(intent);
       }
    }
};
handler.postDelayed(r, 3000);
相关问题