Hi all,
I am making an android racing game. I have a method that draws the track to the screen, the map is bigger than the screen so i clip the bitmap and draw it to the screen hence to draw the track I have to get the visible track points and then draw them. I have noticed that when I enable drawing the track the game lags quite a bit but when i disable it is smooth as expected the code is below if anyone can see any obvious efficiency problems.
public void drawTrack(Canvas canvas) { if (this.trackPoints != null && this.trackPoints.size() > 0) { // get points, convert back to screen coords and add them to array, // then // use that arraylist<point> below getCurrentVisibleTrack(); counter++; if (this.visibleTrackPoints.size() > 0) { // draw lines Point previousPoint = visibleTrackPoints.get(0); Point nextPoint; // draw track line for (int i = 1; i < visibleTrackPoints.size(); i++) { this.p.setColor(Color.BLACK); this.p.setStrokeWidth(100); nextPoint = visibleTrackPoints.get(i); canvas.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y, p); if (i % 2 == 0) { // draw outer track this.p.setColor(Color.WHITE); this.p.setStrokeWidth(5); canvas.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y, p); } previousPoint = nextPoint; } } } } public void getCurrentVisibleTrack() { // clear all elements this.visibleTrackPoints.clear(); // loop track array Point point; for (int i = 0; i < this.trackPoints.size(); i++) { point = this.trackPoints.get(i); // if that piece of track is on the screen currently then add to // array this.visibleTrackPoints.add(new Point((int) (point.x - this.mapx), (int) (point.y - this.mapy))); } }
Thanks in advance!