如何在flutter中显示.9.png格式的图片?

cod*_*ohn 6 android dart flutter

这是我的代码:

new Image.asset('images/launcher/border_highlight.9.png', fit: BoxFit.fill)));
Run Code Online (Sandbox Code Playgroud)

当我改变图像的宽度和高度时,它的显示效果很差,但在原生android中显示正常。

flutter不支持.9图片吗?还有其他方法可以达到同样的效果吗?

use*_*613 9

Flutter 支持类似的概念,但不直接支持 9-patch 文件,因此它实际上适用于任何图像。

例如,我这里有一个 50x50 的图像button.png

用作按钮的图像

当在 Photoshop 中放大到 1200% 时,我们可以看到我们想要定义为“中心”的区域,以便可以拉伸它。我在距每条边 15 像素的位置绘制了一些蓝色引导线:

放大图像

要在 Flutter 中使用它,我们可以使用该centerSlice属性并传入一个 Rect,它基本上定义了我在 Photoshop 中绘制的矩形,即宽度和高度都从 15px 到 35px:

        Image.asset(
          "images/button.png",
          height: 100,
          width: 350,
          centerSlice: Rect.fromLTRB(15, 15, 35, 35),
        )
Run Code Online (Sandbox Code Playgroud)

结果如下(对比不传入centerSlice,而是传入fit: BoxFit.fill):

演示


Gün*_*uer 0

目前 Flutter 支持

JPEG、PNG、GIF、动画 GIF、WebP、动画 WebP、BMP 和 WBMP

另请参阅https://github.com/flutter/engine/blob/e7a294770012800fc8a43494fe646659b496ea49/lib/ui/painting.dart#L21