Canvas.Scale,位图消失了

时间:2013-07-15 18:00:56

标签: android android-canvas

我正在尝试使用Canvas.scale(2f,2f)放大我的Bitmap,但是当我这样做时,图像就会消失。

这是我的代码:

    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();

如果我不缩放它,它会出现在屏幕中间,我希望它在那里。

任何想法?

1 个答案:

答案 0 :(得分:0)

我建议您在缩放之前将画布转换为轴。例如:

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();

这将绘制一个长度为5.0且宽度为5.0的矩形,将长度和宽度缩小为2.5,然后将其移动到(50,50)。Code reference

相关问题