我有一个自定义视图,我在其上绘制了一个坐标 ArrayList(这是一个自定义类,只包含一个 x 和 y 位置)。随着越来越多的坐标被添加到 ArrayList 中,绘图速度明显变慢。我想知道是否有一种更有效的方法来绘制这个 ArrayList,或者,如果我可以只添加一个已添加的坐标(因为 ArrayList 仅在调用无效之间更改一个坐标)。
以下是相关的代码:
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);
}
}
}
谢谢你的帮助!