Magic Marbles
Magic Marbles

Reputation: 403

Canvas won't draw

I don't get it, I have other custom views in which I can draw to canvas perfectly fine. But now the new views I'm creating don't draw to canvas at all! I can't find any discernible difference between what I was doing then and now, it's still the same "canvas.drawLine()" calls afterall.

Here's an example view that will not draw regardless of what I try to do with it (note that the background is black, so the white color should show up)

public class TestView extends View {

    public TestView(Context context)
    {
        super(context);
    }

    public TestView (Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public void onDraw(Canvas canvas){
        Paint paint = new Paint();
        paint.setColor(0xFFFFFF);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);

        canvas.drawLine(0,0, 500, 500, paint);
        canvas.drawCircle(0,0, 40, paint);
        Logy.Print("Drew top:" + getTop() + " left:" + getLeft() + " right:" + getRight() + " bottom:" + getBottom());
    }

}

I've looked at other similar questions, and this issue I'm having is definitely not like the others. I didn't mispell onDraw, onDraw IS being called (I can tell via Logy, which is just a simpler facade to Debug.log()). Please consider this before marking this as duplicate of something else.

The dimensions of the view when onDraw is called is 0,0,800,800 (left, top, right, bottom). onDraw is called at least once when the activity starts. canvas.drawRGB() will flood the view with color.

This view is the only view in the activity, and when I put another "working" custom view in it's place that draws just fine. But now any new custom views I try to get drawing DON'T DRAW.

Upvotes: 0

Views: 1290

Answers (3)

MrSalmon
MrSalmon

Reputation: 89

The colour you are drawing with has an alpha value of 0 (aka transparent), and colour of white. Try drawing with use paint.setColor(0xFFFF3432); or something similarly likely to contrast with your background -- see if you see a red line, you know it is working.

Then choose the colour you wish. If it is white you are going for make sure to use 0xFFFFFFFF not 0xFFFFFF

Colour Format:

0xAARRGGBB

where A = Alpha/Transparency, R = Red, G = Green, B = Blue.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

By assuming that you have provided correct width and height to your view, it seems like your drawing is working right. However there might be a possibility that your activity background is white and the line you are drawing is also white/transparent - causing delusion.

Try changing the paint color to something like:

paint.setColor(Color.Red);

Upvotes: 0

Alex
Alex

Reputation: 1239

You are not overriding the onDraw method. Try this

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //your stuff here
}

Upvotes: 0

Related Questions