我想在ClickListener的for循环中多次重绘customview。但是,视图仅在ClickListener代码完成后刷新一次,而不会在for循环内刷新。
这是相关的代码段
vw = (TouchEventView)findViewById(R.id.TouchEventView);
clear = (Button)findViewById(R.id.button2);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
coords = TouchEventView.getFloats(TouchEventView.points);
if (coords.length < 4) {
Toast.makeText(MainActivity.this,"You must have at least one line",Toast.LENGTH_LONG).show();
}
else if ( coords.length < 6) {
SendData(length(coords[0],coords[1],coords[2],coords[3]));
SendData("s");
}
else {
for (int i = 0; i<coords.length-4; i=i+4) {
SendData(length(coords[i],coords[i+1],coords[i+2],coords[i+3]));
SendData(turn(coords[i],coords[i+1],coords[i+2],coords[i+3],coords[i+6],coords[i+7]));
}
int i = coords.length-4;
SendData(length(coords[i],coords[i+1],coords[i+2],coords[i+3]));
SendData("s");
// Now do the overlay
TouchEventView.retrace = true;
reords = new float[coords.length];
for (int j=0; j<coords.length-1; j=j+4) {
try {
Thread.sleep(1000);
}
catch (Exception e) {
}
// Log.e("pitrack","i : " + j);
TouchEventView.leg = j;
vw.postInvalidate();
}
}
}
});
这是customview(TouchEventView)的onDraw方法
@Override
protected void onDraw(Canvas canvas) {
if (retrace) {
canvas.drawLines(MainActivity.coords,paintRed);
canvas.drawLine(MainActivity.coords[leg],MainActivity.coords[leg+1],MainActivity.coords[leg+2],MainActivity.coords[leg+3],paintGreen);
}
else {
if (points.size() > 3) {
canvas.drawLines(getFloats(points),paintRed);
}
if (x > -1) {
canvas.drawLine(x, y, x1, y1, paintRed);
canvas.drawBitmap(finish,x1-96,y1-96,paintRed);
canvas.drawBitmap(start,startx-48,starty-48,paintRed);
}
}
}
vw.invalidate()和vw.postInvalidate()都不能在ClickListener流程结束时工作。
如何在for循环中强制重新绘制屏幕?
答案 0 :(得分:0)
到目前为止,我发现的唯一解决方案是将customview.postInvalidate()作为可运行程序嵌入到单独的线程中。