开发者

use Handler.post() to drawRect failed

开发者_StackOverflow中文版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);
    }
}

I use Log,it draws,but never showes. Why?


Try this:

    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();

If that doesn't work, could you explain "it draws but never shows".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜