简体   繁体   English

Canvas.Scale,位图消失

[英]Canvas.Scale, bitmap disappeareing

i'm trying to zoom into my Bitmap with Canvas.scale(2f,2f), but when I do it, the image disappear. 我正在尝试使用Canvas.scale(2f,2f)放大我的位图,但是当我这样做时,图像消失了。

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? 有什么想法吗?

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). 这将绘制一个长度为5.0,宽度为5.0的矩形,将其长度和宽度缩小为2.5,然后将其移动到(50,50)。 Code reference . 代码参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM