Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android View: force onDraw() and wait till its done

Tags:

android

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.

like image 963
Eric Avatar asked Nov 22 '25 17:11

Eric


1 Answers

Call invalidate(), but there is no way to wait for the draw to complete. draw will be called later, after your drawLine call returns.

like image 93
yoah Avatar answered Nov 25 '25 08:11

yoah