TomasNM
TomasNM

Reputation: 1

Drawing to canvas while in loop

In Eclipse IDE for Android I am fighting with one simple problem right now. I am using code like this:

public class Reader extends Activity 
{
    // declarations
    private class GraphView extends View 
    {
        protected void onDraw(Canvas canvas) 
        {
            synchronized(this)
            {
                final Paint paint = mPaint;
                // some drawings
                myvoid(canvas,paint);
                // some drawings
            }
        }
    }
    void myvoid(Canvas c,Paint p)
    {
        int i=0;
        do //or for cycle
        {
            i++;
            c.drawText(Integer.toString(i),0,100,p); // <<<<<<<<<<<<<<<< Problem
        }
        while (i<100000);
    }
}

The canvas and text is not updating,until the while loop is finished. It seems,that the canvas is "locked" during that time. How can I achieve,that I can see the progress inside of the loop on the canvas?

Thanks,Tomas.

Upvotes: 0

Views: 3748

Answers (3)

Zielony
Zielony

Reputation: 16537

The problem is that you're not giving any time for Android to present the results of the drawing method. You should draw one step of the loop and call invalidate to trigger next step. Android will draw your text on the screen and then it will try to redraw it with increased counter once again. Please compare your code and my version:

private class GraphView extends View {

    protected void onDraw(Canvas canvas) { // some drawings
        myvoid(canvas, paint);
        // some drawings
    }

    void myvoid(Canvas c, Paint p) {
        int i = 0;
        do // or for cycle
        {
            i++;
            c.drawText(Integer.toString(i), 0, 100, p);
        } while (i < 100000);
    }
}

My version with invalidate():

private class GraphView extends View {
    int i = 0;
    Paint paint = new Paint();

    @Override
    public void draw(Canvas canvas) {
        canvas.drawText(Integer.toString(i), 0, 100, paint);
        i++;
        invalidate();
        super.draw(canvas);
    }
}

Upvotes: 1

hamilton.lima
hamilton.lima

Reputation: 1920

Follow this steps :

  • set the view in the activity
  • start a secondary thread to do your process
  • in the thread invalidate the view

this example maybe helps : https://github.com/hamilton-lima/Bolinha/tree/master/src/com/example/bolinha

Upvotes: 0

Kirtikumar A.
Kirtikumar A.

Reputation: 4204

I think you can try this

imageView = (ImageView) this.findViewById(R.id.imageView1);

        Display currentDisplay = getWindowManager().getDefaultDisplay();
        @SuppressWarnings("deprecation")
        //float dw = currentDisplay.getWidth();
       // float dh = currentDisplay.getHeight();

        bitmap = Bitmap.createBitmap((int) dw, (int) dh,
            Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(Color.RED);
        imageView.setImageBitmap(bitmap);

You can try to make your canvas a bitmap image then set bitmap to your imageview

Upvotes: 0

Related Questions