开发者_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".
精彩评论