简体   繁体   English

使用canvas.scale函数放大画布?

[英]Zoom in a canvas using canvas.scale function?

I have implemented a long onDraw method which draws a set of rectangles. 我已经实现了一个长的onDraw方法,它绘制了一组矩形。 The rectangles are too small and I want them to appear bigger. 矩形太小,我希望它们看起来更大。 But unfortunately I can't change the rectangle coordinates because they are stored in a database. 但不幸的是我无法更改矩形坐标,因为它们存储在数据库中。 So is there any way I can zoom in the canvas using canvas.scale() ? 那么有什么方法可以使用canvas.scale()放大画布?

I'm going to preface this answer by saying you will need to draw everything at 0,0 and then scale it, and finally translate it to behave properly. 我将通过说你需要在0,0处绘制所有内容然后对其进行缩放,并最终将其转换为正常行为来作为前言。

Simply do the following in your onDraw method: 只需在onDraw方法中执行以下操作:

canvas.save();
    canvas.translate(xValue, yValue);
    canvas.scale(xScale, yScale)
    /* draw whatever you want scaled at 0,0*/
canvas.restore();

xScale shrinks or stretches in the X direction, yScale shrinks or stretches in the Y direction. xScale在X方向上收缩或拉伸,yScale在Y方向上收缩或拉伸。

1.0 is the default for these, so 2.0 would stretch it by double and 0.5 would shrink it by half. 1.0是这些的默认值,所以2.0会将它拉伸一倍,0.5会缩小一半。

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)。

The result will be a rectangle drawn as if you did this: 结果将是一个绘制的矩形,就像你这样做:

canvas.drawRect(50.0, 50.0, 52.5, 52.5, paint);

I hope this helps! 我希望这有帮助!

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

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