views:

46

answers:

2

I have the following routine in a subclass of view:

It calculates an array of points that make up a line, then erases the previous lines, then draws the new lines (impact refers to the width in pixels drawn with multiple lines). The line is your basic bell curve, squeezed or stretched by variance and x-factor.

Unfortunately, nothing shows on the screen. A previous version with drawPoint() and no array worked, and I've verified the array contents are being loaded correctly, and I can see that my onDraw() is being triggered.

Any ideas why it might not be drawn? Thanks in advance!

 protected void drawNewLine( int maxx, int maxy, Canvas canvas, int impact, double  variance,  double xFactor, int color) {
  // impact = 2 to 8; xFactor between 4 and 20; variance between 0.2 and 5
  double x = 0;
  double y = 0;
  int cx = maxx / 2;
  int cy = maxy / 2;
  int mu = cx;
  int index = 0;
  points[maxx<<1][1] = points[maxx<<1][0];
  for (x = 0; x < maxx; x++) {
   points[index][1] = points[index][0];
   points[index][0] = (float) x;
   Log.i(DEBUG_TAG, "x: " + x);
   index++;
   double root = 1.0 / (Math.sqrt(2 * Math.PI * variance));
   double exponent = -1.0 * (Math.pow(((x - mu)/maxx*xFactor), 2) / (2 * variance));
   double ePow = Math.exp(exponent);
   y = Math.round(cy * root * ePow);
   points[index][1] = points[index][0];
   points[index][0] = (float) (maxy - y - OFFSET);

   index++;
  }
  points[maxx<<1][0] = (float) impact;

  for (int line = 0; line < points[maxx<<1][1]; line++) {
   for (int pt = 0; pt < (maxx<<1); pt++) {
    pointsToPaint[pt] = points[pt][1];
   }
   for (int skip = 1; skip < (maxx<<1); skip = skip + 2)      
                          pointsToPaint[skip] = pointsToPaint[skip] + line;
   myLinePaint.setColor(Color.BLACK);
   canvas.drawLines(pointsToPaint, bLinePaint); // draw over old lines w/blk
  } 

  for (int line = 0; line < points[maxx<<1][0]; line++) {
   for (int pt = 0; pt < maxx<<1; pt++) {
    pointsToPaint[pt] = points[pt][0];
   }
   for (int skip = 1; skip < maxx<<1; skip = skip + 2)
                           pointsToPaint[skip] = pointsToPaint[skip] + line;
   myLinePaint.setColor(color);
   canvas.drawLines(pointsToPaint, myLinePaint); / new color
  }

 }

update: Replaced the drawLines() with drawPoint() in loop, still no joy

    for (int p = 0; p<pointsToPaint.length; p = p + 2) {
      Log.i(DEBUG_TAG, "x " + pointsToPaint[p] + " y " + pointsToPaint[p+1]);
      canvas.drawPoint(pointsToPaint[p], pointsToPaint[p+1], myLinePaint);
    }
///         canvas.drawLines(pointsToPaint, myLinePaint);
A: 

aren't you suppose to call invalidate (like a mapview) to force the view to reload?

YourView.invalidate() (or maybe postInvalidate(), depending where you are : main sthread or not) here is the detail

Sephy
Actually, it is simpler than that. The first onDraw() command never happens before the end of onStart(). I was driven nuts by this because I was trying to get the view's width and height to initialize the arrays of points, and it absolutely refused to do so... until after onStart().I was thrown off by the examples that show using onDraw() to write to the canvas(), but the light finally dawned when I saw that the first onDraw() never happens until then. You can set up the canvas without knowing view size) and "draw" to your heart's content, but it never _renders_ until the end of onStart().
Donald Scott Wilde
+1  A: 

I was attempting to write from within onCreate() and onStart(). The View and its Canvas are never actually rendered for the first time until the end of onStart().

Donald Scott Wilde