覆盖行为

CoverBehavior 这个 mixin 旨在将纹理渲染到完整控件大小,同时保持原始纹理的宽高比。

用例包括在动态布局中渲染全尺寸背景图像或视频内容。

关于行为的概述,请参阅 behaviors 文档。

示例

以下示例为图像添加了封面行为:

在Python中:

from kivy.app import App
from kivy.uix.behaviors import CoverBehavior
from kivy.uix.image import Image


class CoverImage(CoverBehavior, Image):

    def __init__(self, **kwargs):
        super(CoverImage, self).__init__(**kwargs)
        texture = self._coreimage.texture
        self.reference_size = texture.size
        self.texture = texture


class MainApp(App):

    def build(self):
        return CoverImage(source='image.jpg')

MainApp().run()

在Kivy语言中:

CoverImage:
    source: 'image.png'

<CoverImage@CoverBehavior+Image>:
    reference_size: self.texture_size

详见 CoverBehavior

class kivy.uix.behaviors.cover.CoverBehavior(**kwargs)[源代码]

基类:object

CoverBehavior mixin 提供了一种渲染方式,使纹理覆盖整个控件大小,同时保持原始纹理的宽高比。

在 1.10.0 版本加入.

cover_pos

宽高比感知纹理的位置。在 CoverBehavior.calculate_cover 中计算得出。

cover_pos 是一个 ListProperty,默认值为 [0, 0]

cover_size

宽高比感知纹理的尺寸。在 CoverBehavior.calculate_cover 中计算得出。

cover_size 是一个 ListProperty,默认值为 [0, 0]

reference_size

用于宽高比近似计算的参考尺寸。

reference_size 是一个 ListProperty,默认值为 []