章节知识点总揽
在canvas中,可以使用Translate指令进行平移,除了属性外,使用Translate与Rotate指令基本一样,这里如果不用canvas.before属性就会出现前面两篇的问题
实例:设置平移坐标空间
在使用Translate指令时,需要指定在x轴、y轴和z轴上移动的距离;如果不指定,则默认不移动,修改rotate.kv文件,具体代码如下:
<MyImage@Image>:
source:'01.jpg'
pos:self.parent.pos
size_hint:.5,.4
canvas.before:
PushMatrix
Translate:
x:-100
y:200
z:0
Rotate:
axis:(0,0,1)
angle:60
origin:self.center
Color:
rgba:1,0,0,.5
Line:
rectangle:self.x,self.y,self.width,self.height
canvas.after:
PopMatrix
<RotateGridLayoutWidget>:
cols:2
canvas:
Color:
rgba:(1,1,1,1)
Rectangle:
pos:self.pos
size:self.size
Button:
text:'col:1,row;1'
FloatLayout:
MyImage:
Button:
text:'col:1,row:2'
使用01.jpg图片,放置在main.py相同目录下:
使用上一篇的main.py文件,具体如下:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Rectangle,Color
from kivy.graphics.instructions import InstructionGroup
class RotateGridLayoutWidget(GridLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
class RotateApp(App):
def build(self):
return RotateGridLayoutWidget()
if __name__ == '__main__':
RotateApp().run()
执行结果如下图:
上一篇:基本图形绘制——图像旋转
下一篇:基本图形绘制——缩放坐标空间