Python 为什么Kivy应用程序在Android中被缩小,而不是在开发中
我有这样一段代码,它在我的Linux机器上工作得很好(导入省略): 这让我的背景在笔记本电脑上得到了很好的拉伸,但当我在安卓手机上尝试游戏时,背景图片缩小了 如何对此进行故障排除 建议编辑:Python 为什么Kivy应用程序在Android中被缩小,而不是在开发中,python,android,kivy,Python,Android,Kivy,我有这样一段代码,它在我的Linux机器上工作得很好(导入省略): 这让我的背景在笔记本电脑上得到了很好的拉伸,但当我在安卓手机上尝试游戏时,背景图片缩小了 如何对此进行故障排除 建议编辑: class Background(Widget): def __init__(self, source): super(Background, self).__init__() self.image = Sprite(source=source) s
class Background(Widget):
def __init__(self, source):
super(Background, self).__init__()
self.image = Sprite(source=source)
self.image.allow_stretch = True # <==
self.add_widget(self.image)
self.size = self.image.size
self.image_copy = Sprite(source=source, x=self.width)
self.add_widget(self.image_copy)
class Background(Widget):
def __init__(self, source):
super(Background, self).__init__()
self.image = Sprite(
source=source, allow_stretch=True)
self.add_widget(self.image)
self.size = self.image.size
self.image_copy = Sprite(
source=source, allow_stretch=True, x=self.width)
self.add_widget(self.image_copy)
class Sprite(Image):
def __init__(self, **kwargs):
super(Sprite, self).__init__(**kwargs)
self.size = self.texture_size
x, y = dp(self.size[0]), dp(self.size[1])
self.size = (x, y)
self.size_hint = (None, None)
class Game(Widget):
def __init__(self):
super(Game, self).__init__()
self.background = Background(
source='assets/images/bg.jpg')
self.size = self.background.size
self.add_widget(self.background)
class TrackerWarsApp(App):
def build(self):
game = Game()
Window.size = game.size
self.icon = 'assets/images/logo.png'
return game
在这里,我试图将所有精灵大小(包括背景图片)转换为与像素无关的值。我的背景拉伸过度。将图像的
allow_stretch
属性设置为True
@sevent无效果。由于某些原因,整个游戏都被缩小了。但是,听起来你可能以像素为单位声明了所有大小。手机上的大小差异是否仅仅是因为dpi不同?请使用kivy.metrics.dp转换为与密度无关的像素值。这只是一个近似值,但这是您最容易做到的。除此之外,你还必须决定如何在不同的设备类型上更改应用程序,并从中着手(例如,你可以根据屏幕大小调整所有内容,因此在所有手机上都是相同的)
class Background(Widget):
def __init__(self, source):
super(Background, self).__init__()
self.image = Sprite(
source=source, allow_stretch=True)
self.add_widget(self.image)
self.size = self.image.size
self.image_copy = Sprite(
source=source, allow_stretch=True, x=self.width)
self.add_widget(self.image_copy)
class Sprite(Image):
def __init__(self, **kwargs):
super(Sprite, self).__init__(**kwargs)
self.size = self.texture_size
x, y = dp(self.size[0]), dp(self.size[1])
self.size = (x, y)
self.size_hint = (None, None)
class Game(Widget):
def __init__(self):
super(Game, self).__init__()
self.background = Background(
source='assets/images/bg.jpg')
self.size = self.background.size
self.add_widget(self.background)
class TrackerWarsApp(App):
def build(self):
game = Game()
Window.size = game.size
self.icon = 'assets/images/logo.png'
return game