简体   繁体   English

如何在Canvas Android中缩放位图

[英]How scale bitmap in Canvas android

I want to diasplay image(bitmap) in SurfaceView using Canvas. 我想使用Canvas在SurfaceView中显示图像(位图)。 My requirement is that size of the canvas greater than image.I need space(eg:10 dp) in each side of the image in Canvas. 我的要求是画布的大小要大于图像。在Canvas中,图像的每一侧都需要空间(例如:10 dp)。 How I can scale this? 我该如何扩展?

Thanks Mikahail 谢谢Mikahail

您可以绘制所需的Canvas大小的矩形,然后使用参数(11,11)绘制图像,这将使它从每个尺寸的角开始绘制10px。

Put this method in your SurfaceView Class :D 将此方法放在SurfaceView类中:D

 public void draw(Canvas canvas) {
    final float scaleFactoryX = getWidth() / (WIDTH * 1.f);
    final float scaleFactoryY = getHeight() / (HEIGHT * 1.f);
    if (canvas != null) {
        final int savedState = canvas.save();
        canvas.scale(scaleFactoryX, scaleFactoryY);

        canvas.restoreToCount(savedState);
    }

}

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

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