我刚刚开始学习Kivy,我试图理解Canvas
指令在调整kivyApp窗口大小时是如何受到影响的
在Kivy文档中提到——
Note
Kivy drawing instructions are not automatically relative to the position or size of the widget. You, therefore, need to consider these factors when drawing. In order to make your drawing instructions relative to the widget, the instructions need either to be declared in the KvLang or bound to pos and size changes.
example which follows显示了如何将Rectangle
指令的大小和位置绑定到正在绘制它的小部件的大小和位置。因此,调整窗口大小时,大小和位置会成比例地改变
但是,对于像Bezier
指令这样使用points
的东西,我怎么能做同样的事情呢
我有一个自定义小部件HangManFig1
,它扩展了Widget
类,该类在KVlang
中定义如下:
<HangManFig1>:
canvas:
Line:
points: (150, 100, 150, 700)
width: 10
Line:
points: (150, 700, 600, 700)
width: 10
Line:
points: (150, 600, 250, 700)
width: 10
Line:
points: (600, 700, 600, 600)
width: 3
Ellipse:
pos: (550, 500)
Line:
bezier: (610, 510, 630, 400, 570, 350)
width: 10
Line:
bezier: (570, 350, 510, 370, 450, 270)
width: 10
Line:
bezier: (570, 350, 600, 300, 550, 200)
width: 10
Line:
bezier: (610, 480, 530, 430, 500, 430)
width: 10
Line:
bezier: (610, 480, 630, 500, 680, 390)
width: 10
我以Screen
的方式使用这个小部件,方式如下:
#:import HangManFig1 figures.hangmanfig1
<MainScreen>:
name: '_main_screen_'
BoxLayout:
RelativeLayout:
size_hint_x: 0.7
HangManFig1:
RelativeLayout:
size_hint_x: 0.3
Button:
text: 'Play'
pos_hint: {'x': 0.1, 'y': 0.80}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_game_screen_'
Button:
text: 'Practice'
pos_hint: {'x': 0.1, 'y': 0.60}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_game_screen_'
Button:
text: 'Share'
pos_hint: {'x': 0.1, 'y': 0.40}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_share_screen_'
Button:
text: 'Credits'
pos_hint: {'x': 0.1, 'y': 0.20}
size_hint: (0.8, 0.1)
on_release:
root.manager.transition.direction = 'left'
root.manager.current = '_credits_screen_'
当我调整窗口的大小时,我看到虽然Buttons
的位置正确,但没有HangManFig1
是否有一种方法可以将此小部件的大小绑定到父小部件的大小,以便即使窗口大小发生变化,它也能正确定位
虽然您使用RelativeLayout使指令的坐标相对于小部件的位置,但它对其大小没有任何影响
当你用数值对所有的位置进行硬编码时,你需要一种方法来缩放这些值相对于你的小部件的大小,并且你必须考虑在这种情况下,你想要在行的宽度上发生什么,如果它相对于控件的大小也增长了呢?线性?还有别的吗?根据您的需要,存在各种可能性最简单的起点,IMHO,是使用缩放指令,相对于小部件的大小设置所有画布指令,而不是您用来设计当前绞刑架的大小
如果这对您来说还不够,例如,因为您希望保持行的宽度不变,您可以不使用Scale指令,而是使用一个函数,将小部件的大小和一组坐标作为输入,并返回相对于该大小的值:
这个函数可以这样使用
如果你想要更复杂的东西,比如保持绞刑架的长宽比,同时保持在你的尺寸范围内,你可以相应地调整如下:
是的,你可以。这需要做一些工作,但不是为}使用显式坐标,而是使用基于
Canvas
{HangManFig1
对象大小的值。例如,图形的第一个Line
可能类似于:我有:
这可能有点混乱,可能包含一些不必要的内容。那是因为,首先我想让它更先进。但后来有点疯狂了。但这仍然是一个做矢量线的好方法。它支持宽度和您在行中提供的其他属性(我希望如此)。颜色不会改变,但可以实现。让我们看看它的实际行动:
这篇文章比其他的好吗?嗯,这可以使用Bézier curves,而不是点,并且是响应性的
奇怪的是,它的性能并没有那么差。 当您调整窗口大小时,它将触发。所以,剩下的时间,就像基本行+几个字节的RAM来保存相对值和附加值一样
相关问题 更多 >
编程相关推荐