2014-08-29 29 views
0

我正在为我创建的练习应用程序创建一个简单的启动画面。我有一堆不可见的图像,我希望在延迟5秒内显示,并使用Handler Runnable进行设置。这是到目前为止我的代码:Android - 使用Handler Runnable显示视图

long secondsDelayed = 5; 

    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      //I want to display this images in 2 second count 
      ivLogoText.setVisibility(View.VISIBLE); 
      ivLogoStem.setVisibility(View.VISIBLE); 

      //I want to display this image in 3 second count 
      ivSmLeaves.setVisibility(View.VISIBLE); 

      //I want to display this image in 4 second count 
      ivMedLeaves.setVisibility(View.VISIBLE); 

      //I want to display this image in 5 second count 
      ivLargeLeaves.setVisibility(View.VISIBLE); 

      startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class)); 
      finish(); 
     } 
    }, (long) (secondsDelayed * 1000)); 
} 

从我现在的代码,启动画面停留5秒这正是我所想要的,但所有的图像会以5秒计数显示了太多。有人可以帮我弄这个吗?

回答

0

当你想在2秒,3secs,4秒,5秒,以显示不同的图像,你需要有多个这样的可运行在交错发布倍。或者您可以使用倒数计时器,您可以计算滴答数量并执行不同的操作。

如果你想用你的方法:

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      //I want to display this images in 2 second count 
      ivLogoText.setVisibility(View.VISIBLE); 
      ivLogoStem.setVisibility(View.VISIBLE); 
} 
    }, (long) (2 * 1000)); 

new Handler().postDelayed(new Runnable() { 

      //I want to display this image in 3 second count 
      ivSmLeaves.setVisibility(View.VISIBLE); 
} 
    }, (long) (3 * 1000)); 

new Handler().postDelayed(new Runnable() { 

      //I want to display this image in 4 second count 
      ivMedLeaves.setVisibility(View.VISIBLE); 
    }, (long) (4 * 1000)); 

new Handler().postDelayed(new Runnable() { 

      //I want to display this image in 5 second count 
      ivLargeLeaves.setVisibility(View.VISIBLE); 

      startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class)); 
      finish(); 
     } 
    }, (long) (5 * 1000)); 
+0

我该怎么做@Maddy? – 2014-08-29 04:09:54

+0

http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,long) – yiati 2014-08-29 04:32:08

+0

Thanks @Maddy。它工作得很好。我只是需要浏览更多,但适当的执行这个。推荐一些新手阅读? – 2014-08-29 04:45:02

0

您可以使用ThreadSleep内螺纹的Runnable。里面Thread,您可以用活动的runOnUiThread功能更改UI(图像视图)如下代码:

new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       Thread.sleep(2000); // wait 2s 
       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         //show hide image view here. 
        } 
       }); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

     } 
    }).start(); 
0

尝试是这样的

 new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       switch (step) { 
        case 1: 
         //I want to display this images in 2 second count 
         ivLogoText.setVisibility(View.VISIBLE); 
         ivLogoStem.setVisibility(View.VISIBLE); 
         break; 
        case 2: 
         //I want to display this image in 3 second count 
         ivSmLeaves.setVisibility(View.VISIBLE); 
         break; 
        case 3: 
         //I want to display this image in 4 second count 
         ivMedLeaves.setVisibility(View.VISIBLE); 
         break; 
        case 4: 
         //I want to display this image in 5 second count 
         ivLargeLeaves.setVisibility(View.VISIBLE); 
         break; 
        case 5: 
         startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class)); 
         finish(); 
         return;// there is no more step 
        case 0: 
         // TODO: do something afeter 1 second 
        default: 
       } 
       ++ step; 
       // reserve next step 
       handler.postDelayed(this, 1000); 
      } 
     }, 1000);