KIVY:RecycleView 中的 AsyncImage 小部件调整大小问题

KIVY: AsyncImage widget resizing issue within a RecycleView

在解释我的问题之前,我会先了解一些背景信息。

我实现了一个包含 RecycleBoxLayout 的 RecycleView 小部件,其 default_size 是动态的(基于其父级的大小)。我认为这是必需的(我可能错了),因为我的应用程序将用于各种设备,而且我希望我的 RV 平均包含 4 个 PlaylistItem。这是 .kv 文件中的代码:

RecycleView:
    id: rv
    scroll_type: ['bars', 'content']
    scroll_wheel_distance: dp(114)
    viewclass: 'PlaylistItem'
    RecycleBoxLayout:
        default_size: None, (self.parent.height / 4 - dp(80)) if self.parent.height / 4 > dp(60) else dp(60)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        spacing: dp(20)
        padding: dp(20)

现在让我们看一下 PlaylistItem 视图类的代码:

<PlaylistItem@BoxLayout>:
    created_time: ''
    description: ''
    id: ''
    image_url: ''
    name: ''
    owner: ''
    updated_time: ''
    playlist_name: ''

    MDCard:
        AsyncImage:
            size_hint_x: .2
            size: self.texture_size
            source: root.image_url
            mipmap: True

        BoxLayout:
            orientation:'vertical'

            MDLabel:
                text: root.playlist_name
                theme_text_color: 'Secondary'
                font_style:'Title'     
            MDSeparator:
                height: dp(1)
            MDLabel:
                text: 'Body'
                theme_text_color: 'Primary'

所以,我想在 AsyncImage 小部件中保持图像的比例。我希望这张图片的宽度占其父窗口小部件的 20%。最后我希望 AsyncImage 的大小等于它的图像(纹理)大小。上面的代码是这样做的,但是根据 window 的大小,图像的高度并不总是等于其父级的高度(结果不太好呃)。当我尝试时:

AsyncImage:
    size_hint_y: None
    height: self.parent.height
    source: root.image_url
    mipmap: True

图像的高度总是很好(等于 MDCard 高度)但 AsyncImage 的宽度可能会变得非常大。这是一张图片,让您更好地理解:

我能想到的唯一解决方法是:

<PlaylistItem@BoxLayout>:
    created_time: ''
    description: ''
    id: ''
    image_url: ''
    name: ''
    owner: ''
    updated_time: ''
    playlist_name: ''

    # I ADDED THIS # <----------------------------------
    size_hint_y: None
    miminum_height: self.minimum_height

    MDCard:
        AsyncImage:
            size_hint_x: .2
            size: self.texture_size
            source: root.image_url
            mipmap: True

这是行不通的。我相信在初始化时纹理还没有加载,因此 self.minimum_height 是 None。对此我不确定。希望有人能帮忙。

奖励详情:我确信我所有的原始图像都有 480 像素的高度

非常感谢您的关注,抱歉这么久post。

Atis 在#kivy IRC 频道上找到了解决方案。应该简单地使用 image_ratio!:

AsyncImage:
    size_hint: None,None
    size:self.image_ratio*root.height,root.height
    source: root.image_url
    mipmap: True