使用Thread使ImageView数组中的元素可见

时间:2015-01-12 10:39:38

标签: android multithreading imageview

我有一个ImageView数组,其中包含许多不可见的元素,我希望它们可以顺序显示(即使第一个可见,等待半秒,使下一个可见,等等)使用ThreadrunOnUiThread

我尝试了以下内容:

Thread th = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(500);
            im1.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im2.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im3.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im4.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im5.setVisibility(View.VISIBLE);
            Thread.sleep(1000);
            bottom.setVisibility(View.VISIBLE);                 
        } catch (Exception e) {
            Log.e("ERR", e.getMessage());
        }
    }
});
th.start();

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(500);
            im1.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im2.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im3.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im4.setVisibility(View.VISIBLE);
            Thread.sleep(500);
            im5.setVisibility(View.VISIBLE);
            Thread.sleep(1000);
            bottom.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            Log.e("ERR", e.getMessage());
        }
    }
});

但我没有达到预期的效果。所发生的是所有图像同时可见,而不是一个接一个地显示。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

int counter=1; //Global variable

private void visibleImageview()
    {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                if(counter==1)
                {
                     im1.setVisibility(View.VISIBLE);
                }
                else if(counter==2)
                {
                    im2.setVisibility(View.VISIBLE);
                }
                else if(counter==3)
                {
                    im3.setVisibility(View.VISIBLE);
                }


                ++counter;
                if(couter<=3)
                visibleImageview();
            }
        }, 3000);
    }

答案 1 :(得分:0)

解决了它。这就是我设法做到的方式:

Handler handler = new Handler();
im1.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                im2.setVisibility(View.VISIBLE);
            }
        });

    }
},500);

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                im3.setVisibility(View.VISIBLE);
            }
        });

    }
},1000);

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                im4.setVisibility(View.VISIBLE);
            }
        });

    }
},1500);

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                im4.setVisibility(View.VISIBLE);
            }
        });

    }
},2000);

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bottom.setVisibility(View.VISIBLE);
            }
        });

    }
},3000);