问题
I'm creating an android app that has to draw a map of an ambient that i have explored with laserscans. I have e text file with all my data like:
index x y
path 0 0 0
path 1 1 0
path 2 2 0
...etc
obstacle 0 10 10
obstacle 1 10 22
..etc
so I have xy coordinates of where I've been and xy of obstacles I've seen.
I have a thread
that reads the data from the text file and stores that data in a list
.
Another thread
reads that list
and draws all the points that are put in the list
until that moment by the reading thread
.
My problem is that I don't want to re-read everything every time the reading thread
has put new data into the data list
. There is a way to draw something like a bitmap and modify this dynamically? I mean that every time I have read some new data I "open" the bitmap, I add to that the new points, "close" that bitmap and show on the screen?
what I am doing now is to read all the list in my onDraw()
function and draw point by point, but I have 170 000 points and that is a useful work because every time the points are in the old position, I only have some new points...
回答1:
You can create a bitmap and a canvas in your view and just continue to draw into this bitmap as necessary. To prevent points from being drawn over again, the thread that draws the points should either remove points from the list as they are drawn, or keep track of the index of the last point.
Here's an example that contains the basics:
public class myView extends View {
Bitmap mBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(mBitmap);
Paint mPaint = new Paint();
public void updateBitmap(List<Point> points) {
while (!points.isEmpty()) {
int x = points.get(0).x;
int y = points.get(0).y;
mCanvas.drawPoint(x, y, mPaint);
points.remove(0);
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
The thread that draws the points calls updateBitmap
, passing it the current list of points to draw. These points are then removed from the list so they will not be drawn again later on.
来源:https://stackoverflow.com/questions/9032214/best-way-to-draw-an-image-dynamically