简体   繁体   English

Kivy如何旋转图片

[英]Kivy how to rotate a picture

我正在尝试旋转一些我必须在屏幕上显示的图片,这些图片在stacklayout内,我需要将它们显示为Portrait而不是landscape,我正在使用Image Widget Thanks

The previous 2 answer of toto_tico is a way to do, but i would rather create a new widget for it, and use it: toto_tico之前的2个答案是一种方法,但我宁愿为它创建一个新的小部件,并使用它:

Builder.load_string('''
<RotatedImage>:
    canvas.before:
        PushMatrix
        Rotate:
            angle: root.angle
            axis: 0, 0, 1
            origin: root.center
    canvas.after:
        PopMatrix
''')

class RotatedImage(Image):
    angle = NumericProperty()

Then, use this widget as other Image widget, you just have a "angle" property you can play with. 然后,使用此小部件作为其他图像小部件,您只需要一个“角度”属性即可玩。

Note: the collision detection is not handled on the image, except in the scatter example. 注意:除散点示例外,图像上不处理碰撞检测。 Scatter can be expensive just for rotate something, but at least the collision works. 对于旋转物体而言,散射可能很昂贵,但至少碰撞是有效的。

I don't think the Scatter is meant to be use for this. 我不认为Scatter是用于此目的。 But I guess is a more intuitive solution. 但我想这是一个更直观的解决方案。 The Scatter includes a rotation (and also a scale) property. Scatter包括旋转(以及缩放)属性。

Basically, I embedded the Image inside a Scatter and use the rotation property to rotate 90 degrees. 基本上,我将Image嵌入Scatter中并使用rotation属性旋转90度。

Why do I say the Scatter is not meant for this task. 为什么我说Scatter不适用于此任务。 Basically because it allows gestures over it. 基本上是因为它允许手势。 You can basically translate, rotate or scale with your fingers (or using the multi-touch mouse emulation ). 您可以用手指进行平移,旋转或缩放(或使用多点触控鼠标仿真 )。 That is why in the next example I am setting the do_scale , do_rotation and do_translation to false. 这就是为什么在下一个例子中我将do_scaledo_rotationdo_translation为false。 I am clarifying this before you get confuse with the do_rotation: false 在你混淆do_rotation: false之前我会澄清这个do_rotation: false

from kivy.app import App
from kivy.uix.stacklayout import StackLayout
from kivy.lang import Builder

Builder.load_string("""
<Example>:
    Image:
        source: 'kivy.png'
        size_hint: None,None
        size: 64,64
    Scatter:
        pos: 0,0
        size_hint: None,None
        size: 64,64
        do_rotation: False
        do_scale: False
        do_translation: False
        rotation: 90
        Image:
            source: 'kivy.png'
            size_hint: None,None
            size: 64,64

""")

class Example(App, StackLayout):
    def build(self):
        return self

if __name__ == "__main__":
    Example().run()

I think they are two ways of doing this. 我认为他们有两种方法可以做到这一点。 I'll post two answers and let others decide what is the right approach. 我将发布两个答案,让其他人决定什么是正确的方法。 I personally prefer this method because I think it's computational lighter. 我个人更喜欢这种方法,因为我认为它的计算更轻。 However, it is not that intuitive 但是,它并不直观

This method uses a RelativeLayout and two context instructions (Rotate and Translate). 此方法使用RelativeLayout和两个上下文指令(Rotate and Translate)。

1 - You need to embed the Image inside the RelativeLayout . 1 - 您需要将Image嵌入RelativeLayout中 Why? 为什么? Because the way Rotate works is similar to putting a nail in the (0,0) coordinate, ie bottom-left corner. 因为Rotate的工作方式类似于在(0,0)坐标(即左下角)放置钉子。 The RelativeLayout sets the 0,0 to the position of the Widget. RelativeLayout将0,0设置为Widget的位置。

2- You will need to use the canvas 2-您需要使用画布

3- As I said before, the Rotate instruction is equivalent to put a nail in the (0,0) coordinate. 3-正如我之前所说,旋转指令相当于在(0,0)坐标中放置钉子。 Think about a piece of paper. 想想一张纸。 If you put a nail in the corner the rotation is going to end on the left. 如果你在角落里钉一个钉子,旋转就会在左边结束。 So, before the rotation, you need to Translate the piece of paper to your right. 因此,在轮换之前,您需要将纸张翻译到右侧。

4- Now you can Rotate the RelativeLayout and It will end in the position you are expecting. 4-现在您可以旋转 RelativeLayout,它将以您期望的位置结束。

There is another advantage of using a RelativeLayout. 使用RelativeLayout还有另一个优点。 It already includes two important instructions ( PushMatrix and PopMatrix ) that you must understand if you are extensively working with rotating, scaling or translating. 它已经包含两个重要指令( PushMatrixPopMatrix ),如果您正在广泛使用旋转,缩放或翻译,则必须了解这些指令。

Here is an example code: 这是一个示例代码:

from kivy.app import App
from kivy.uix.stacklayout import StackLayout
from kivy.lang import Builder

Builder.load_string("""
<Example>:
    Image:
        source: 'kivy.png'
        size_hint: None,None
        size: 64,64
    RelativeLayout
        size_hint: None,None
        size: 64,64
        canvas.before:
            Translate:
                x: 64
            Rotate:
                angle: 90
                axis: 0,0,1
        Image:
            source: 'kivy.png'
            size_hint: None,None
            size: 64,64
""")

class Example(App, StackLayout):
    def build(self):
        return self

if __name__ == "__main__":
    Example().run()

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

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