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

↘锁芯ラ 提交于 2019-12-01 11:48:57

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!