How to get View.invalidate working within Click Listener

135 Views Asked by At

I want to redraw a customview multiple times within a for loop in a ClickListener. However, the view is only refreshed once on completion of the ClickListener code, not within the for loop.

Here is the relevant code snippet

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

and here is the onDraw method of the customview (TouchEventView)

    @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);
        }
    }
}

Neither vw.invalidate() nor vw.postInvalidate() work other than at the end of the ClickListener process.

How can I force a redraw of the screen within the for loop?

2

There are 2 best solutions below

0
On

The only solution I have found so far is to embed the customview.postInvalidate() as a runnable in a separate thread.

0
On

It is a shady workaround BUT you can force the view to invalidate, if you add another onclickListener off somekind. It worked for me when i came to showing a Progressbar while uploading something in the main thread (i had to do it that way). It is not pretty but it is working. Good Luck with that.