简体   繁体   中英

Android Drawing Multiple Circles without the old ones disappearing

class MyView extends View
{
Paint dotPaint;
boolean mContextVariable;
List<Point> points = new ArrayList<Point>();
Point p = new Point();
public MyView(Context context) 
{
    super(context);

     dotPaint = new Paint();
     dotPaint.setColor(Color.YELLOW);

}

@Override
protected void onDraw(Canvas canvas)
{
    canvas.drawARGB(100, 100, 100, 255);
    int xCenter = canvas.getWidth()/2;
    int yCenter = canvas.getHeight()/2;
    Functions fc = new Functions();
    MainActivity layout = new MainActivity();
    int coordmax = (fc.countChar(layout, "coordsWalked.pkm", "§") - 1) / 2;
    invalidate();
    int loop = 1;
    while (loop <= coordmax){
        int lat = Integer.parseInt(fc.getDataSplit(layout, "coordsWalked.pkm", ((coordmax*2)-1)))+xCenter;
        int lng = Integer.parseInt(fc.getDataSplit(layout, "coordsWalked.pkm", ((coordmax*2))))+yCenter;
        p.x = lat;
        p.y = lng;
        points.add(p);
        loop++;
    }
    for(Point p: points){
           canvas.drawCircle(p.x, p.y, 10, dotPaint);
           invalidate();
      }
      invalidate();
}

*"coordsWalked.pkm" is a text file that has a series of coordinates separated by a splitter so that *2 - 1 is the x coordinate and 2 - 0 is the y coordinate. No need to worry about this too much, all of my circles are being drawn at exactly the correct coordinates, and thus in the correct place.

The problem is that it only draws a circle at the last coordinate in the text file. What I want is for it to draw circles for every single one of the coordinates in the file and show them at the same time, but instead it only ever shows the last circle. I tried to solve this using the for(Point p: points) solution I found online, but it still runs exactly the same as before. I've also tried tweaking the invalidates, turning them off and on etc, but it makes no apparent difference.

I've sifted through tons of answers, but none of them work for me. Some of them don't work because they use different methods to draw the circles that I don't understand or is not practical for me to use (like using surfaceView), and for others I have no idea why they don't work. The code I have now is partially from another answer I found online that didn't work for me.

I checked the answer about not calling invalidate() and confirmed that calling it does indeed null out the previous graphics. So far so good. This matches your observation.

But it looks like you are setting up a custom View. A previous discussion suggests the answer - if indeed your real problem is that your onDraw() method is not being called. This answer is from the link:

Why onDraw is not called after invalidate()? [from RGraham's solution]

You need to call setWillNotDraw(false); in order for your onDraw method to be called:

 private void init() { setWillNotDraw(false); ... } 

Hope this helps! Cheers,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM