(安卓)为什么会不坏()来更新我的按钮,立即? [英] (Android) Why won't invalidate() update my buttons immediately?

查看:106
本文介绍了(安卓)为什么会不坏()来更新我的按钮,立即?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几个论坛和示例使用无效(),以实时更新的观点,但我还是不明白,为什么我在做什么都不行。下面的code使用由定义图像按钮红,蓝,绿色和黄色。我把每一次我尝试和改变一个按钮的外观之间有1秒的延迟。请有人告诉我,我做错了。

I have read several forums and examples on using invalidate() in order to update views immediately but I still do not understand why what I am doing will not work. The code below uses image buttons defined by "red", "blue", "green", and "yellow". I set a 1 second delay between each time I try and change a button's appearance. Please someone tell me what i'm doing wrong.

private void showPattern() {
    if (correct == true) {
        for (int k = 0; k < temp_basket.length; k++) {
            if (temp_basket[k] == 0) {
                red.setPressed(true);
                red.invalidate();
                final Handler handler = new Handler();
                Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        handler.post(new Runnable() {
                            public void run() {
                                red.setPressed(false);
                                red.invalidate();
                            }
                        });
                    }
                }, 1000);

有3个或以后这一个是蓝,绿,黄和这些块

There are 3 more or these blocks after this one that are blue, green, and yellow.

推荐答案

Invaliadate把一个重绘消息队列

Invaliadate puts a redraw message in the queue

我在你的code看到,有多个重画后,其他发生在...操作系统将尝试通过泡吧多个重绘讯息合二为一(条件是有之间没有其他消息下优化渲染他们)。

As I see in your code, there are multiple redraws happening on after the other... the OS will try to optimize the rendering by clubbing multiple redraw messages into one (under the condition that there's no other message between them).

你可能想要做的是:

private Handler myHandler = new Handler() {
   public void handleMessage(Message msg)
   {
      switch(msg.what) { /* do your work */ }
   }
};

Message msg = Message.obtain(myHandler);
msg.what = <some-number>;
msg.obj = <your-data-to-process>

if(myHandler.containsMessage(<same-number>) {
   myHandler.removeMessages(<same-number>);
}
myHandler.sendMessage(msg);

这篇关于(安卓)为什么会不坏()来更新我的按钮,立即?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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