Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution I ended up using involved some algebra in the class where I extended <code>View</code>.</p> <p>The function gets the array of scores (values between 0 and 10) from the activity and uses the values two-by-two as start and end points to a line (note that the end point of one line is the start point of the next line). </p> <p>It calculates the length of the line and how many segments will be in the line; also how far to move in the <code>x</code> direction and how for to move in the <code>y</code> direction. </p> <p>Then it calculates the <code>x</code> and <code>y</code> values of the end of the first segment in the line and draws that segment. </p> <p>Then the <code>xDirection</code> and <code>yDirection</code> is added to the <code>x</code> and <code>y</code> points respectively and the line is drawn again, which now includes the first and second segment of the line. This is done for every segment in the line, and then the final line from <code>point A</code> to <code>point B</code> is drawn. </p> <p>But it's not complete - the entire <code>for loop</code> in the <code>setData</code> function should be placed in a recursive function, because <code>postInvalidateDelayed()</code> doesn't pause the <code>for loop</code> from executing. </p> <p>However, nothing is drawn on the canvas at all, hence the link to my other question currently on SO: <a href="https://stackoverflow.com/questions/17170453/why-is-my-lines-not-drawn-on-my-custom-view-canvas">Why are no lines drawn on my (custom View) canvas?</a></p> <p>But for this question, I think that the solution I ended up using is probably not so bad. Comments?</p> <pre><code>public void setData(float[] scorePoints, float max, int totalScores){ Log.d(TAG, "Get stuff from activity"); scores = scorePoints; numberOfScores = totalScores; Log.d(TAG, "Number of scores = " + numberOfScores); maxScore = max; Log.d(TAG, "Max score = " + maxScore); segmentToDraw = (float) 10; //get the width of the area to draw width = Math.abs(getWidth() - getPaddingLeft() - getPaddingRight()); //get the height of the area to draw height = getHeight() - getPaddingTop() - getPaddingBottom(); Log.d(TAG, "Drawable area in view = " + width + " x " + height); /* * Now we start filling an array of points. * The onDraw function will drawLines of groupings of four points in a given array. * * For the first segment, we'll draw from the x and y of the first point (which will be in the 1st and 2nd spots in our array) * to the x and y of the first segment (which will be in the 3rd and 4th spots in our array). * And then the 3rd and 4th spots in our array will be replaced by a new x and y * marking the end of the second segment to be drawn from our first point. * * This will happen until the x and y is not smaller than the x and y of the final point of the line, any more. * Then the 3rd and 4th spots in our array will be replaced by the x and y of the final point of the line. * * If there are more points to draw, the 5th and 6th spots in our array will also be created and filled with * the x and y of the final point of the line because it'll be the first two values (x and y) for drawing the next line. * * So, yes, there will be duplicates in our array of points to draw, but a grouping of four spots will be used to draw one line, * and the end point of the first line is the starting point of the second line, so we need it twice. */ points.add(getXPos(scores[0])); points.add(getYPos(scores[0])); points.add((float) 0); points.add((float) 0); x = points.get(0); y = points.get(1); startPoint = scores[0]; endPoint = scores[1]; for(int i=0; i&lt;scores.length-1; i++){ String thePoints = ""; if(i&gt;0){ startPoint = scores[i]; endPoint = scores[i+1]; x = points.get(i*4); y = points.get((i*4) + 1); } startPointX = getXPos(startPoint); startPointY = getYPos(startPoint); endPointX = getXPos(endPoint); endPointY = getYPos(endPoint); distanceOfLine = (float) Math.sqrt(Math.pow((endPointX - startPointX), 2) + Math.pow((endPointY - startPointY), 2)); Log.d(TAG, "Distance of line = " + distanceOfLine); //get number of segments in line numberOfSegmentsInLine = (int) (distanceOfLine/segmentToDraw); Log.d(TAG, "Number of segments in line = " + numberOfSegmentsInLine); //get distance to move in Y direction yDirection = (float) ((endPointY - startPointY)/ (float) numberOfSegmentsInLine); Log.d(TAG, "Move " + yDirection + " in Y direction"); //get distance to move in X direction xDirection = (float) ((endPointX - startPointX)/ (float) numberOfSegmentsInLine); Log.d(TAG, "Move " + xDirection + " in X direction"); //loop for each segment for(int j=0; j&lt;numberOfSegmentsInLine; j++){ x += xDirection; y += yDirection; points.set(points.size()-2, Float.valueOf(x)); points.set(points.size()-1, Float.valueOf(y)); Log.d(TAG, "Line : " + (i+1) + " Segment : " + j); Log.d(TAG, "X = "+ (x+xDirection) + " Y = " + (y+yDirection)); Log.d(TAG, "Call invalidate now!"); //invalidate(); //postInvalidateDelayed(delayMilliseconds); } //draw final line points.set(points.size()-2, endPointX); points.set(points.size()-1, endPointY); invalidate(); //postInvalidateDelayed(delayMilliseconds); if(i&lt;scores.length-2){ points.add(endPointX); points.add(endPointY); points.add((float) 0); points.add((float) 0); } for(int k =0; k&lt;points.size(); k++){ thePoints = thePoints + " : " + points.get(k); } Log.d(TAG, "Points array = " + thePoints); } } @Override public void onDraw(Canvas canvas){ //setWillNotDraw(true); Log.d(TAG, "DRAW DAMNIT!!!"); Log.d(TAG, "Width = " + (int) width + " Height = " + (int)height); paint = new Paint(); paint.setStyle(Style.STROKE); paint.setStrokeWidth(4); paint.setColor(Color.RED); //paint.setAntiAlias(true); //paint.setShadowLayer(4, 2, 2, 0x81000000); Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888); canvas = new Canvas(bitmap); String drawPointsGo = ""; float[] drawPoints = new float[points.size()]; for(int i=0; i&lt;points.size(); i++){ Float f = points.get(i); drawPoints[i] = (float) (f != null ? f : 0.0); drawPointsGo = drawPointsGo + " : " + drawPoints[i]; } Log.d(TAG, "Draw Points: " + drawPointsGo); canvas.drawLines(drawPoints, paint); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload