Justin Warner
Justin Warner

Reputation: 879

Android canvas drawing in incorrect order... Why/how to fix it?

So, basically, if a bitmap exists, draw it first, stretched accross whole view/canvas. Then draw some text and a rectangle over top of that after, and it should work great. Right?

I was under the assumption that this would draw a "background" (Being currentPicture) and then draw some stuff below it, the % and the rectangle.

Wondering why it's not behaving like such? It was working before, but something changed and now it doesn't.

Further, the:

canvas.drawRect(0, 0, width, 200, p); 

does not draw.

Similar story with:

canvas.drawRect(0, 100, width, 200, p); 

both of which are in the if statement. I believe the first one shouldn't be drawn, but the second one should. The bitmap is drawn correctly.

Any questions, please ask!

Code:

@Override
public void onDraw(Canvas canvas) {
    int myColor = 0;
    p.setColor(Color.TRANSPARENT);
    canvas.drawRect(0, 0, width, height, p);
    if (currentPicture != null) {
        p.setColor(Color.RED);
        canvas.drawRect(0, 0, width, 200, p);
        canvas.drawBitmap(currentPicture, new Rect(0, 0, width, height), new Rect(0, 0, width, height), p);
        canvas.translate(translateX, translateY);
        canvas.scale(scaleX, scaleY);
        pictureCanvas = canvas;
        p.setColor(Color.GREEN);
        canvas.drawRect(0, 100, width, 200, p);
    }
    if (ci >= 66) {
        myColor = (Color.RED);
    } else if (ci >= 33) {
        myColor = (Color.YELLOW);
    } else {
        myColor = (Color.GREEN);
    }
    p.setColor(myColor);
    // Progress bar stuff.
    canvas.drawRect(0, height - 100, (progress * width / 20), height, p);
    // backdrop for textview.
    p.setColor(Color.BLACK);
    canvas.drawText(ci + "%", -1, height - 19, p);
    canvas.drawText(ci + "%", +1, height - 21, p);
    // Draw CI.
    p.setColor(myColor);
    p.setTextSize(100);
    canvas.drawText(ci + "%", 0, height - 20, p);
}

Upvotes: 2

Views: 1565

Answers (1)

WarrenFaith
WarrenFaith

Reputation: 57682

p.setColor(Color.TRANSPARENT);
canvas.drawRect(0, 0, width, height, p);

are pretty nonsense especially when you draw a complete background after that.

Have you tried to remove the translate/scale to see if this might be the issue?

Oh, and why are you doing that: pictureCanvas = canvas;?

Upvotes: 1

Related Questions