Reputation: 703
I got a View and want to update it every time a new line object is stored in my lines array. This happens at drawLine(). Therefore drawYourselfe() should somehow force onDraw() and wait till its done. Here is a part of my code. Can anyone tell me how drawYourselfe() should look?
public class MyView extends View{
private ArrayList<Line> lines;
private Paint paint; //created in constructor
protected void drawLine(float[] f)
{
if(f.length != 5)
{
} else {
lines.add(new Line(f));
Log.d("SuM","added Line");
if(!bufferedDrawing) // in this example bufferedDrawing=false because i want to update the View on every new Line;
{
drawYourselfe();
}
}
}
protected void onDraw(Canvas canvas) {
Log.d("SuM","onDraw");
for(Line l : lines)
{
l.analysis(); //Logs fromX,toX,fromY,toY and color to console
paint.setColor(l.getColor());
canvas.drawLine(l.getFromX(),l.getFromY(),l.getToX(),l.getToY(), paint);
}
}
public void drawYourselfe()
{
//When this is called, onDraw should be called and the Thread should wait till its done;
}
}
Edit: I better mention what I want to do and why just simply invalidate() doesnt work: In my MainActivity I call
myView.drawLine(line1);
myView.drawLine(line2);
myView.drawLine(line3);
//and so on...
and I want to see how these lines are drawn one after one. When calling invalidate() in drawYourselfe() I only see all lines drawn but not one after one. I know that these tree lines are drawn so fast that I cant see them drawn one by one, but in the app Im doing it with thousand of lines.
Upvotes: 3
Views: 5810
Reputation: 53
Please call invalidate() function when add a new line. invalidate will call onDraw function
Upvotes: 0
Reputation: 7230
Call invalidate(), but there is no way to wait for the draw to complete. draw will be called later, after your drawLine call returns.
Upvotes: 1
Reputation: 11131
call View.invalidate()
to force the View
to redraw it's contents and as there is only one Thread
here, it will wait until drawing is completed i.e. next instruction will not be executed until onDraw()
is completed...
Upvotes: 0