0

i'm trying to zoom into my Bitmap with Canvas.scale(2f,2f), but when I do it, the image disappear.

Here's my code:

    int srcLeft = GodFrameWidth * GodCurAnimation;
    int srcTop = 0;
    int srcRight = (GodFrameWidth * GodCurAnimation) + GodFrameWidth;
    int srcBottom = GodFrameHeight;

    float destLeft = CanvasWidth/2 - GodFrameWidth;
    float destTop = CanvasHeight/2;
    float destRight = destLeft + GodFrameWidth;
    float destBottom = destTop + GodFrameHeight;

    RectF dst = new RectF(destLeft, destTop,destRight ,destBottom );
    Rect src = new Rect(srcLeft,srcTop,srcRight,srcBottom);
    canvas.save();
    canvas.scale(2f, 2f);
    canvas.drawBitmap(GodMap, src, dst,Pencil);
    canvas.restore();

if I don't scale it, it's appearing right in the middle of the screen, where I want it to be.

any ideias?

Jarrod
  • 9,349
  • 5
  • 58
  • 73
Moondustt
  • 864
  • 1
  • 11
  • 30

1 Answers1

0

I propose to you that translate your canvas to the pivot before scale it.For example:

canvas.save();
canvas.translate(50, 50);
canvas.scale(0.5f, 0.5f);
canvas.drawRect(0.0, 0.0, 5.0, 5.0, paint);
canvas.restore();

This will draw a rectangle with length 5.0, and width 5.0, scale it down to 2.5 for length and width, and then move it to (50, 50).Code reference.

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167