Android - 使用Handler Runnable显示视图

时间:2014-08-29 03:58:54

标签: java android runnable

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

    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秒内显示出来。有人可以帮我这个吗?

4 个答案:

答案 0 :(得分:0)

这是因为您正在同时更改所有图像的可见性。

而不是这个,你应该使用View animator

http://developer.android.com/reference/android/widget/ViewAnimator.html

答案 1 :(得分:0)

由于你想在2秒,3秒,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));

答案 2 :(得分:0)

您可以在帖子的Thread内使用SleepRunnable。在Thread内,您可以使用活动的runOnUiThread功能更改用户界面(您的图片视图),如下所示:

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

答案 3 :(得分: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);