Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient onDraw of ArrayList

I have a custom View on which I draw an ArrayList of Coordinates (which is a custom class just containing an x and y position). As more and more Coordinates are added to the ArrayList, the drawing noticeably slows down. I was wondering if there's a more efficient way to draw this ArrayList, or alternatively, if I can just add the one Coordinate that was added (because the ArrayList only changes one Coordinate between calls to invalidate).

Here are the relevant pieces of code:

public class CustomDraw extends View {
// member variables

public void updateLine() {
    // grab new coordinates for each measure

    if(measure1.isEmpty()) {
        measure1.add(new Coordinate(0, 0));
    } else {
        Coordinate last_coord = measure1.get(measure1.size() - 1);

        // calculations for south, north, east, and west

        if(south && east) {
            measure1.add(new Coordinate(last_coord.x + 3, last_coord.y + 3));
        } else if(south && west) {
            measure1.add(new Coordinate(last_coord.x - 3, last_coord.y + 3));
        } else if(north && east) {
            measure1.add(new Coordinate(last_coord.x + 3, last_coord.y - 3));
        } else if(north && west) {
            measure1.add(new Coordinate(last_coord.x - 3, last_coord.y - 3));
        }
    }

    if(draw) {
        dh.sleep(10);
    }
}


@Override
public void onDraw(Canvas c) {
    super.onDraw(c);
    Paint p = new Paint();
    p.setStyle(Paint.Style.FILL);

    p.setColor(Color.WHITE);
    c.drawPaint(p);
    p.setColor(Color.BLACK);

    switch(mSelected) {
    case Constants.MEASURE_1:
        for(int i = 0; i < measure1.size(); i++) {
            Coordinate coord = measure1.get(i);
            Log.d("MAAV", "drawing coord.x, coord.y: " + (coord.x) + ", " + (coord.y));
            c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);  
        }
        break;
    }

}

class DrawHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {
        CustomDraw.this.updateLine();
        CustomDraw.this.invalidate();
    }

    public void sleep(long delayMillis) {
        this.removeMessages(0);
        sendMessageDelayed(obtainMessage(0), delayMillis);
    }
}
}

Thanks for any help!

like image 640
Jon Rubins Avatar asked Apr 18 '12 20:04

Jon Rubins


1 Answers

You are declaring coord each iteration of the loop. You don't need to do this and allocations of memory for objects can be expensive. Move that outside the loop and simply reuse the object. Also try commenting out the log calls or only log on each 10th item.

Coordinate coord;
for(int i = 0; i < measure1.size(); i++) {
   coord = measure1.get(i) 
   if (i%10==0)
       Log.d("MAAV", "drawing coord.x, coord.y: " + (coord.x) + ", " + (coord.y));
   c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);  
}

If these updates don't improve performance enough consider using OpenGL ES to do the drawing.

like image 158
slayton Avatar answered Oct 20 '22 00:10

slayton