marienke
marienke

Reputation: 2475

Why are no lines drawn on my (custom View) canvas?

I'm trying to draw a lines from points in a float[]. I know that the points get to the drawLines() function because I log them here:

Draw Points: : 154.18182 : 784.8889 : 215.27272 : 677.3333 : 215.27272 : 677.3333 : 337.45453 : 462.22217 : 337.45453 : 462.22217 : 276.36365 : 569.7778 : 276.36365 : 569.7778 : 398.54544 : 354.66663 : 398.54544 : 354.66663 : 154.18182 : 784.8889 : 154.18182 : 784.8889 : 337.45453 : 462.22217 : 337.45453 : 462.22217 : 520.7273 : 139.55554 : 520.7273 : 139.55554 : 581.8182 : 32.0 : 581.8182 : 32.0 : 398.54544 : 354.66663 : 398.54544 : 354.66663 : 154.18182 : 784.8889

*Note: pay no attention to the duplication of points - the end point of the first line is the start point of the second line. The drawLines function requires four consecutive points, hence the duplication of points.

My width and height are valid integers: Width = 672 Height = 968

My onDraw function:

Note that I've already tried a few things which are now commented out. My background is grey, so the Color.RED line should be visible, when it's ever drawn.

@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<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);
}

My XML:

<za.co.widge.test.linegraph.LineGraphView 
    android:id="@+id/linechart"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:background="#444"
    android:padding="16dp"
    android:layout_weight="1"
    />

Result: The grey block appears on the phone's screen, but no red line is drawn.

Why is nothing drawn on my (custom View) canvas?

Upvotes: 0

Views: 535

Answers (1)

arcone1
arcone1

Reputation: 234

Remove this lines:

Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);

You are overwriting canvas instance passed to your onDraw method with offscreen (bitmap backed) one, so nothing is plot on screen.

Upvotes: 1

Related Questions