How can I efficiently draw many pixels on a Canvas?
<p>I'm making my first game using Java on Android. I need to draw a lot of pixels which together should create a line. My first approach was to make a large array of booleans, create a loop, and draw a pixel when the associated boolean was true.</p>

<p>It wasn't a good idea of course (the array is about 200x300). Now I remember only the position of the first pixel of the line, and every next pixel has to remember his follower. It works pretty well, but when the line gets longer (but still not very long), the efficiency is bad (&lt;20 fps after 4000 frames).</p>

<p>This is the function that I use to draw a line (only one for now). Can anybody help me improve its efficiency?</p>

<pre><code>public void drawLine(Canvas canvas, int beginx, int beginy) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);

    int x = beginx;
    int y = beginy;
    while(C.mGrid[x][y].nx != -1) {
        //canvas.drawLine(x, y, C.mGrid[x][y].nx, C.mGrid[x][y].ny, paint);
        canvas.drawPoint(x, y, paint);
        Grid temp = C.mGrid[x][y];
        if ((C.mGrid[x][y].nx == x) &amp;&amp; (C.mGrid[x][y].ny == y)) break;
        x = temp.nx;
        y = temp.ny;
    }
}
</code></pre>

<p>and Grid.java:</p>

<pre><code>package com.qwak.achtung;

public float x = 0,y = 0;
public int px = -1, py = -1, nx = -1, ny = -1;

public Grid(float x, float y) {
    this.x = x;
    this.y = y;
}

public void set(int px, int py, int nx, int ny) {
    this.px = px;
    this.py = py;
    this.nx = nx;
    this.ny = ny;
}

public void setp(int px, int py) {
    this.px = px;
    this.py = py;
}

public void setn(int nx, int ny) {
    this.nx = nx;
    this.ny = ny;
}
</code></pre>

<p>PS: It looks like this <a href="http://c.wrzuta.pl/wi10559/11f7d10b00110e504e25ebd3/0/andek" rel="nofollow">http://c.wrzuta.pl/wi10559/11f7d10b00110e504e25ebd3/0/andek</a> 14 is fps (on my phone (samsung Spica) it run better - 40 but after a while it decreases to 20 and even less) and 983 is number of frames at all. </p>
