the_candyman
the_candyman

Reputation: 1663

Android - Can't draw a bitmap in a canvas

I have the following code that works:

protected void onDraw(Canvas canvas) {    
    canvas.drawRect(undoButtonRectF, buttonPaint);
    canvas.drawPath(undoButtonArrow, buttonArrowPaint);

Anyway, when I exchange this with the following:

protected void onDraw(Canvas canvas) {        
        undoButtonBitmap = Bitmap.createBitmap((int) undoButtonRectF.width(), (int) undoButtonRectF.height(), Bitmap.Config.ARGB_8888);

        Canvas ca = new Canvas(undoButtonBitmap);

        ca.drawRect(undoButtonRectF, buttonPaint);
        ca.drawPath(undoButtonArrow, buttonArrowPaint);

        canvas.drawBitmap(undoButtonBitmap, undoButtonRectF.left, undoButtonRectF.top, buttonPaint);

nothing is drawn. How can this happen? Am I using bitmap in a wrong way? Please, help me!!!

Upvotes: 0

Views: 214

Answers (1)

Zephyr
Zephyr

Reputation: 6341

I suppose there is a coordinator error.

The error is that undoButtonRectF contains the offsets for "canvas", not for "ca", you should adjust or remove the top/left offset before you call

 ca.drawRect(undoButtonRectF, buttonPaint);
 ca.drawPath(undoButtonArrow, buttonArrowPaint);

Right ?

Upvotes: 1

Related Questions