如何强制变化的图的图像调用setImageResource后? [英] How to force the change the image of a view immediately after calling setImageResource?

查看:267
本文介绍了如何强制变化的图的图像调用setImageResource后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想立即改变的ImageView的形象后,我点击它。

I'm trying to change the image of a ImageView immediately after I click it.

我试着用 .setImageDrawable 并调用 .invalidate()上来看,没有任何成功。

I tried using .setImageDrawable and calling .invalidate() on that view, without any success.

我可能没有这样做是正确的,但我试图单击图像,把主题到睡眠 2秒钟,然后显示其他的图像给用户

I'm probably not doing it right, but I'm trying to click the image, put the Thread to sleep for 2 seconds then display another image to the user.

在code是非常简单的,它是这样的:

The code is quite simple, and it's something like this:

myImage.setImageResource(R.id.myPicture);
... computations
... put the thread to sleep for 2 seconds
myImage.setImageResource(R.id.newPicture);

和用户不能看到myPicture为那些2秒睡眠,然后将其改变为newPicture

and the user can't see "myPicture" for those 2 seconds of sleep, and then it changes to "newPicture"

推荐答案

您可以使用处理程序 sendMessageDelayed()方法C>在一定的时间具体数额的延迟执行任何任务。写逻辑到处理器中setImageDrawable

You can use sendMessageDelayed() method of handler to perform any task at a delay of some specific amount of time. and write the logic to setImageDrawable inside that handler

查看样品code

@Override
    public void onClick(View v) {

        Message msg = new Message();
        photoGridHandler.sendMessageDelayed(msg, 2000);// delay of 2000 milisecond = 2 second

    }


    private Handler photoGridHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            photoGrid.setImageDrawable(myImage);

        };
    };

这篇关于如何强制变化的图的图像调用setImageResource后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆