Android canvas.translate做什么?

Android canvas.translate做什么?,android,Android,这里可以找到一个例子。 翻译-基本上按照它说的做。用x,y平移画布。如果要绘制两个对象,其中一个对象只是另一个对象的平移,例如,每个点的x2=x1+50。您不必对第二个对象再次进行所有计算,但只需平移画布并再次绘制相同的对象即可。我希望这将对您有所帮助。它将改变您的canvaz(刻度除外)的x或y位置 如果我们翻译和缩放,那么它就是一般术语中的转换即使在几年前我第一次回答这个问题时,我也不太理解画布转换(如翻译,旋转等)是如何工作的。我曾经认为,translate移动了你正在绘制的东西。实际上,

这里可以找到一个例子。
翻译-基本上按照它说的做。用x,y平移画布。如果要绘制两个对象,其中一个对象只是另一个对象的平移,例如,每个点的x2=x1+50。您不必对第二个对象再次进行所有计算,但只需平移画布并再次绘制相同的对象即可。我希望这将对您有所帮助。

它将改变您的canvaz(刻度除外)的x或y位置
如果我们翻译和缩放,那么它就是一般术语中的转换

即使在几年前我第一次回答这个问题时,我也不太理解
画布
转换(如
翻译
旋转
等)是如何工作的。我曾经认为,
translate
移动了你正在绘制的东西。实际上,
translate
移动整个坐标系。这也可以达到移动正在绘制的对象的预期效果

在屏幕上,看起来您正在移动图形:

实际情况是将坐标系移动到画布上的新位置:

我首先在
(0,0)
处绘制树。然后我将坐标系的原点转换到画布上的其他点。然后我在
(0,0)
处再次绘制树。这样,我的绘图代码根本不需要更改任何内容。只有坐标系会改变

通常
(0,0)
位于视图的左上角。执行
Canvas.translate
将其移动到视图的其他部分

保存和恢复坐标系 您可以执行
save()
restore()
以返回原始坐标系

// draw the tree the first time
canvas.drawBitmap(tree, 0, 0, mPaint);

// draw the tree the second time
canvas.save();
canvas.translate(dx, dy); // dx = change in x, dy = change in y
canvas.drawBitmap(tree, 0, 0, mPaint); // draw still thinks it is at (0,0)
canvas.restore(); // undo the translate 
当您还原时,图形已在画布上。恢复并不能改变这一点。它只是将坐标系移回保存时所在的位置

为什么要翻译 请注意,通过更改绘制方法的x、y坐标,可以实现相同的效果:

// draw the tree the first time
canvas.drawBitmap(tree, x, y, mPaint);

// update the x,y coordinates
x += dx;
y += dy;

// draw the tree the second time
canvas.drawBitmap(tree, x, y, mPaint);
从数学背景来看,这可能更直观。但是,在平移、旋转和缩放图像时,通常很容易保持绘图逻辑不变,只需变换画布。重新计算每个绘图的
x
y
可能非常昂贵。

想象一下这是一个打印头。 解释这一点最简单的方法是想象它是喷墨打印机或2D打印机的打印头

translate
基本上是将打印头沿X轴和Y轴“移动”到指定的像素数

结果位置成为新的“原点”(0,0)

现在我们了解了这一点,让我们做一个简单的面孔,方法如下:

起始原点:

x
canvas.drawRect(10, 10, 10, 10, paint);

x__
|__|
canvas.translate(20, 0)

 __    x    
|__|
canvas.drawRect(10, 10, 10, 10, paint);

 __    x__
|__|   |__|
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.

 __     __
|__|   |__|

x
canvas.drawLine( 0, 0, 30, 0, paint ); 
 __     __
|__|   |__|

x___________
canvas.translate(0, 20)
 __     __
|__|   |__|

___________

x
x
将(大致)表示原点(或打印头)的位置

为左眼画一个矩形:

x
canvas.drawRect(10, 10, 10, 10, paint);

x__
|__|
canvas.translate(20, 0)

 __    x    
|__|
canvas.drawRect(10, 10, 10, 10, paint);

 __    x__
|__|   |__|
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.

 __     __
|__|   |__|

x
canvas.drawLine( 0, 0, 30, 0, paint ); 
 __     __
|__|   |__|

x___________
canvas.translate(0, 20)
 __     __
|__|   |__|

___________

x
注意:“原点”没有改变,它仍然在这个矩形的左上角

将“原点”向右移动20点:

x
canvas.drawRect(10, 10, 10, 10, paint);

x__
|__|
canvas.translate(20, 0)

 __    x    
|__|
canvas.drawRect(10, 10, 10, 10, paint);

 __    x__
|__|   |__|
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.

 __     __
|__|   |__|

x
canvas.drawLine( 0, 0, 30, 0, paint ); 
 __     __
|__|   |__|

x___________
canvas.translate(0, 20)
 __     __
|__|   |__|

___________

x
使用完全相同的矩形命令绘制右眼:

x
canvas.drawRect(10, 10, 10, 10, paint);

x__
|__|
canvas.translate(20, 0)

 __    x    
|__|
canvas.drawRect(10, 10, 10, 10, paint);

 __    x__
|__|   |__|
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.

 __     __
|__|   |__|

x
canvas.drawLine( 0, 0, 30, 0, paint ); 
 __     __
|__|   |__|

x___________
canvas.translate(0, 20)
 __     __
|__|   |__|

___________

x
将“原点”移回原始X位置并沿Y轴向下移动:

x
canvas.drawRect(10, 10, 10, 10, paint);

x__
|__|
canvas.translate(20, 0)

 __    x    
|__|
canvas.drawRect(10, 10, 10, 10, paint);

 __    x__
|__|   |__|
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.

 __     __
|__|   |__|

x
canvas.drawLine( 0, 0, 30, 0, paint ); 
 __     __
|__|   |__|

x___________
canvas.translate(0, 20)
 __     __
|__|   |__|

___________

x
然后画一张嘴来结束它:

x
canvas.drawRect(10, 10, 10, 10, paint);

x__
|__|
canvas.translate(20, 0)

 __    x    
|__|
canvas.drawRect(10, 10, 10, 10, paint);

 __    x__
|__|   |__|
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.

 __     __
|__|   |__|

x
canvas.drawLine( 0, 0, 30, 0, paint ); 
 __     __
|__|   |__|

x___________
canvas.translate(0, 20)
 __     __
|__|   |__|

___________

x
现在只需将“原点”向下移动20点即可展示我们的杰作:

x
canvas.drawRect(10, 10, 10, 10, paint);

x__
|__|
canvas.translate(20, 0)

 __    x    
|__|
canvas.drawRect(10, 10, 10, 10, paint);

 __    x__
|__|   |__|
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.

 __     __
|__|   |__|

x
canvas.drawLine( 0, 0, 30, 0, paint ); 
 __     __
|__|   |__|

x___________
canvas.translate(0, 20)
 __     __
|__|   |__|

___________

x

不适合缩放,因为使用单空格字体只能做这么多事情。:)

我还是不明白它是干什么的。你能解释一下为什么他们要做翻译,画然后撤销翻译吗?好的,你有一张画布,你想在上面画两棵树和第一棵树上面的一只鸟。这些树的y坐标相同,但x坐标不同(例如50英寸)。那你打算怎么办。。。。继续绘制前三棵树,然后向右移动画布50英寸(canvas.translate就是这样做的),并绘制第二棵树。为了恢复画布的原始位置,您将再次将其移动50英寸,但这次向左移动。现在,当你画鸟的时候,它会在第一棵树的上方,如果你不恢复画布的原始状态,你在翻译后画的所有对象都会被翻译成——在我们的例子中,鸟会画在第二棵树的上方,你不必这样做。第一次处理这些事情有点让人困惑,但一旦你习惯了,事情就变得简单了。完全同意,这个解释真让我受不了!干得好。这是到目前为止最好的解释,现在在画了几幅画之后,它变得有点复杂了。有没有办法重置到原点?@Lamar谢谢。不确定是否有内置方法,但您可以手动跟踪它,以便每次使用translate时,都更新局部变量以显示“您离起点有多远”。然后,如果你想回到最初的起点,你可以给我们一个翻译来扭转这些运动。是的,这就是我最后所做的。谢谢你的帮助。我还以为你的名字叫约书亚·普林蒂普呢。我先看了你的答案,突然想到是打印机。顺便说一句,回答得很好。我实际上想知道save()、translate()和restore()都做了些什么。这就是答案。对于将来仍在检查的人来说,translate只会移动画布坐标,而不会移动你在上面画的任何东西。所以先翻译,然后画:)