章节知识点总揽
实例:上一篇种使用了Rotate指令后,其后的整个坐标都发生了变化,导致最后一个Button按钮也随着旋转。为了避免类似情况,可以通过以下代码将坐标空间旋转回来。
由于上述情况很少出现,在一般情况下会使用两个新的命令,分别如下:
1.PushMatrix:保存当前的上下文环境。
2.PopMatrix:恢复最后保存的上下文环境。
关于PushMatrix和PopMatrix命令的使用,可参考下面部分代码:
<MyImage@Image>:
source:'01.jpg'
pos:self.parent.pos
size_hint:.5,.4
canvas:
PushMatrix
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
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()
运行结果,按钮位置未发生变化,只是图片进行了旋转。
上一篇:基本图形绘制——旋转坐标空间
下一篇:基本图形绘制—— 图像旋转