why ImageView can't update before SystemClock.sleep()

前端 未结 1 616
不知归路
不知归路 2021-01-16 15:11

I want to show other image in ImageView within 3 second, after that rollover old image. The code:

    OnClickListener oc = new OnClickListener() {
@Override
         


        
相关标签:
1条回答
  • 2021-01-16 15:19

    You are blocking the UI thread. Thus during the sleep command, the screen won't refresh. What you need is to schedule a non-blocking delayed call to a function which changes image resource. Here is a modified code that would do such a thing:

    Handler mHandler = new Handler(); /*handler declared in your Activity thread, I assume*/
    
    OnClickListener oc = new OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView iv = (ImageView)v;
            iv.setImageResource(img2_id);
    
            mHandler.postDelayed(new Runnable(){
                public void Run(){
                    iv.setImageResource(img1_id);
                }
            },3000);
    
        }
    }
    myImageView.setOnClickListener(oc);
    
    0 讨论(0)
提交回复
热议问题