如何使用sleep更改ImageView的可见性

时间:2016-02-19 20:29:08

标签: android

我试图通过更改它在Activity中的可见性来显示ImageView几秒钟(带睡眠)。我该怎么做到这一点?

我的代码看起来像这样

    Button button;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.mainButton1);
        imageView = (ImageView) findViewById(R.id.mainImageView1);

        button.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View p1)
                {
                    startProgress(imageView);
                }

        });


    }

    public void startProgress(View v)
    { 
        // do something long 
        Runnable runnable = new Runnable() {
            @Override public void run()
            {
                imageView.setVisibility(ImageView.VISIBLE);
                doFakeWork();
                imageView.setVisibility(ImageView.INVISIBLE);
            } 
        }; 
        new Thread(runnable).start();
    } 

    // Simulating something timeconsuming 

private void doFakeWork()
{
    try
    { 
        Thread.sleep(2000);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace(); 
    } 
}

单击按钮后应用程序崩溃。我究竟做错了什么?

编辑: 错误日志

02-19 22:49:53.153 8804 8870 W     dalvikvm com.mycompany.myapp                 threadid=13: thread exiting with uncaught exception (group=0x419a7c08)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           FATAL EXCEPTION: Thread-27996
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           Process: com.mycompany.myapp, PID: 8804
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7081)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1107)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at android.view.ViewGroup.invalidateChild(ViewGroup.java:4529)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at android.view.View.invalidate(View.java:11809)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at android.view.View.setFlags(View.java:9711)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at android.view.View.setVisibility(View.java:6294)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at android.widget.ImageView.setVisibility(ImageView.java:1247)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at com.mycompany.myapp.MainActivity$100000001.run(MainActivity.java:44)
02-19 22:49:53.188 8804 8870 E     AndroidRuntime com.mycompany.myapp           at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:1)

这样做的一种方式是:

public void startProgress(){

   //make the image visible
   imagView.setVisibility(ImageView.VISIBLE);

   //start a handler that will wait for 2000ms and then make the img invisible
   Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                imagView.setVisibility(ImageView.INVISIBLE);
            }
        }, 2000);
}