首页 > 解决方案 > 为什么用旋转调整窗口大小会干扰 UI 布局,包括 kivy 中的图像

问题描述

旋转图像并调整窗口大小后,图像未移动假定位置。在我的假设中,图像总是移动到左侧疼痛的中心。请告诉我解决方案。

该代码实现了在左侧和右侧生成带有疼痛(区域窗口)的窗口。左侧被假定为菜单栏。假设右侧通过旋转来操纵图像。如果您在窗口上移动鼠标,图像将根据您的鼠标距离旋转。

我的环境。

from cmath import sqrt
from kivy.app import App
from kivy.core.window import Window
from kivy.graphics.context_instructions import LoadIdentity
from kivy.input import MotionEvent
from kivy.properties import NumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.lang import Builder

Builder.load_string("""
<Example>:
    orientation:"horizontal"
    BoxLayout:
        orientation:"vertical"
        size_hint:0.5,1 # rate of win, this is menubar.
        canvas.before:
            Color:
                rgba: 0, 0, 0.5, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            size:1,0.5
        Button:
            size:1,0.5
    FloatLayout:
        id: near_layout
        pos_hint:  {'x':0.5,'y':0}
        size_hint:0.5,1
        canvas.before:
            Color:
                rgba: 0, 1, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Image:
            id:img
            center: near_layout.center

            canvas.before:
                Rotate:
                    angle: root.angle
                    axis: 0,0,1
                    origin: self.center
            source: 'kivy.jpeg'
""")


class Example(App, BoxLayout):
    angle = NumericProperty(0)

    def build(self):
        Window.bind(on_resize=self.on_resize)
        return self

    def on_resize(self, obj, x, y):
        LoadIdentity()
        self.img = self.ids.img  # type:Image
        self.img.reload()  # Necessary,if not, image disappeared

    def on_touch_move(self, touch):
        self.angle += sqrt((touch.ppos[0] - touch.pos[0]) ** 2 + (touch.ppos[1] - touch.pos[1]) ** 2).real
        print("pos:%s,size:%s" % (self.ids.near_layout.pos, self.ids.near_layout.size))

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

标签: pythonkivykivy-language

解决方案


推荐阅读