如何用我的图像填充整个矩形,使其适合整个屏幕作为背景?
public static InputStream getFileStream(IPath pluginRelativeFilePathToGet) {
URL fileURL = yourPlugin.getBundle()
.getResource(pluginRelativeFilePathToGet.toOSString());
URLConnection connection = fileURL.openConnection();
return connection.getInputStream();
}
现在它只是在左下角显示一个小图像作为默认值。我不知道如何调整图像大小或使图像居中,因为我不知道中心的坐标是什么,图像没有调整大小。
答案 0 :(得分:3)
矩形设置为MyPaintWidget的大小,在父窗口小部件上以默认大小绘制。如果MyPaintWidget是根小部件,那么设置self.size将允许它占用整个窗口空间。 (请注意,当前结构只会在on_touch_down事件上调整画布大小。因此,如果调整窗口大小,则需要单击以调整图像大小。)
#Change MyPaintApp to the following...
class MyPaintApp(App):
def build(self):
return MyPaintWidget()
您还可以创建一个单独的小部件来保存背景。下面我添加了一个用于背景的MyBackground小部件,当屏幕大小发生变化时,它必须调整大小。还有其他几种方法可以做到这一点。
from random import random
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.graphics import Color, Line, Rectangle
from kivy.uix.filechooser import FileChooserListView, FileChooserIconView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
class MyBackground(Widget):
def __init__(self, **kwargs):
super(MyBackground, self).__init__(**kwargs)
with self.canvas:
self.bg = Rectangle(source='water.png', pos=self.pos, size=self.size)
self.bind(pos=self.update_bg)
self.bind(size=self.update_bg)
def update_bg(self, *args):
self.bg.pos = self.pos
self.bg.size = self.size
class MyPaintWidget(Widget):
def on_touch_down(self, touch):
color = (random(), random(), random())
with self.canvas:
Color(*color)
d = 30.
touch.ud['line'] = Line(points=(touch.x, touch.y))
def on_touch_move(self, touch):
touch.ud['line'].points += [touch.x, touch.y]
class MyPaintApp(App):
def build(self):
parent = MyBackground()
painter = MyPaintWidget()
parent.add_widget(painter)
return parent
if __name__ == '__main__':
MyPaintApp().run()
答案 1 :(得分:1)
很容易写入.kv文件。
<MyPaintWidget>
canvas:
Rectangle:
pos: self.pos
size: self.size
source: 'water.png'