使用Handler.post()来drawRect失败

时间:2011-03-24 08:42:13

标签: android multithreading user-interface

public class TestView extends View
{
public TestView(Context context)
    {
        super(context);
        handler=new Handler();
    }
protected void onDraw(Canvas canvas)
    {
        this.canvas=canvas;
        Thread thread=new Thread(null,new Runnable()
        {
            public void run()
            {
                    int length=32;
                    paint=new Paint();
                    paint.setColor(Color.WHITE);
//now I want to draw something.
//I use the main UI thread
                    handler.post(new Runnable()
                    {
                        public void run()
                        {
                            draw();
                        }
                    });
                }
            },"thread",262144);
            thread.start();
        }
    }
private void draw()
    {
        canvas.drawRect(length,length,length+length,length+length,paint);
    }
}

我使用Log,它绘制,但从不显示。 为什么呢?

1 个答案:

答案 0 :(得分:0)

试试这个:

    Thread thread=new Thread(null,new Runnable() {
        public void run() {
                int length=32;
                paint=new Paint();
                paint.setColor(Color.WHITE);
               //now I want to draw something.
               //I use the main UI thread
                handler.post(new Runnable() {
                    public void run() {
                        draw();
                    }
                });
            }
        },"thread",262144);
    }.start();

如果这不起作用,你能否解释一下“它吸引但从未表现出来”。