KIVY:AsyncImage窗口小部件在RecycleView中调整大小问题
问题描述:
在解释我的问题之前,我先从一些背景知识开始。KIVY:AsyncImage窗口小部件在RecycleView中调整大小问题
我实现了一个RecycleView小部件,其中包含一个RecycleBoxLayout,其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 viewClass类代码:
<[email protected]>:
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的大小等于它的图像(纹理)大小。上面的代码根据窗口的大小做了这个,但图像的高度并不总是等于它的父母的高度(结果不是很漂亮)。当我尝试:
AsyncImage:
size_hint_y: None
height: self.parent.height
source: root.image_url
mipmap: True
图像的高度总是很好(等于MDCard高度),但AsyncImage的宽度可能会变得非常大。这里有一个图片,让你更好的理解:
我能想到的唯一的解决方法是:
<[email protected]>:
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
这是行不通的。我相信在init的时候纹理还没有加载,因此self.minimum_height是None。对此我不确定。希望有人能帮助。
奖金的细节:我肯定知道我所有的原始图像具有480个像素高度
非常感谢您的关注和遗憾的长期职位。
答
Atis在#kivy IRC频道找到了解决方案。应该简单地使用image_ratio!:
AsyncImage:
size_hint: None,None
size:self.image_ratio*root.height,root.height
source: root.image_url
mipmap: True