前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter--资源管理

Flutter--资源管理

作者头像
None_Ling
发布2019-03-11 14:59:39
1.8K0
发布2019-03-11 14:59:39
举报
文章被收录于专栏:Android相关Android相关

Flutter的资源类型

Flutter可以添加代码以及assets到APP中。而每个Asset都是被打包在发布的APP中的,并且在APP运行时可以访问这些资源。

通用的Asset类型包括:

  • JSON文件
  • 配置文件
  • 图标
  • 位图(JPEG、Webp、Gif、Animated Gif/Webp、PNG)

指定Asset

Flutter使用项目根目录下的pubspec.yaml文件来指定APP所需要的资源。

  • 单个文件指定:
flutter:
  assets:
    - assets/my_icon.png
    - assets/background.png
  • 文件夹指定: 通过指定目录名+/字符即可,只有在该目录下的所有文件可以被包括,如果该目录还有子目录的话,则需要添加一个新的Entry。
flutter:
  assets:
    - assets/
    - assets/image/

Asset Variants

构建系统支持Asset Variants的概念: 在不同的环境下,需要显示不同版本的资源。

例如,日夜间模式的资源,资源名相同,但是环境不同。

当一个资源的路径在pubspec.yaml文件的assets Section中指定的时候,构建系统就会在相邻的子目录中查找相同的名称的资源文件。而查找到的这些文件也会被打到Asset Bundle中。

例如:有一个background.png文件,在日夜间都需要使用,graphics中存放日间资源,而dark中存放夜间资源。

  .../pubspec.yaml
  .../graphics/my_icon.png
  .../graphics/background.png
  .../graphics/dark/background.png
  ...etc.

而在pubspec.yaml文件中,将background.png添加到assets的Section中。

flutter:
  assets:
    -  graphics/background.png

最终,在打包的时候会把.../graphics/background.png.../graphics/dark/background.png都打到Bundle中。而前一个被认为是Main Bundle,而后一个则认为是Variant Bundle

而如果使用:

flutter:
  assets:
    - graphics/

myicon.pngbackground.png/dark/background.png也都会打到Bundle中。

Flutter目前使用Asset Variant来解决图片适配的问题,而未来这种机制也会应用在不同的语言等其他地方。

加载Assets

APP可以通过AssetBundle对象来访问资源。

  • 加载String/Text:通过loadString方法
  • 加载图片/二进制文件:通过load方法

而在Build的阶段,逻辑Key会根据pubspec.yaml文件中的路径来进行映射。

每个Flutter App都有一个rootBundle对象来方便的访问主资源Bundle。可以通过package:flutter/services.dard中的全局静态变量rootBundle来直接访问资源。不过还是推荐使用当前的BuildContext来获取DefaultAssetBundle,通过调用DefaultAssetBundle.of(context)来获取。

而当没有Context的Widget,则需要通过rootBundle来获取。

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;

Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/config.json');
}

图片加载的适配

Flutter会根据当前设备的设备像素比(device Pixel Ratio)来选择图片。

AssetImage知道如何映射到最相近的设备像素比的图片,为了让Mapping能够更好的工作,Assets应该有这种结构:

  .../my_icon.png
  .../2.0x/my_icon.png
  .../3.0x/my_icon.png

默认主资源Bundle中是1.0x的图片。

例如,当前设备的设备像素比是1.8,则会选择/2.0x/my_icon.png。如果是2.7时,则会选择/3.0x/my_icon.png

如果Image控件的宽高都没有指定的话,通常的解决方案是进行资源压缩,然后和主资源Bundle中的图占据相同的像素空间。

例如,1.0x的my_icon.png是72px * 72px,而3.0x的my_icon.png是216px * 216px,但是它需要绘制到72px * 72px的Image控件上,如果这个控件的宽高没有指定的话。

加载图片

在Widget的build函数中使用AssetImage类来加载图片。

Widget build(BuildContext context) {
  // ...
  return DecoratedBox(
    decoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage('graphics/background.png'),
        // ...
      ),
      // ...
    ),
  );
  // ...
}

与平台共享资源

Flutter的Asset资源也可以与Android/Ios共享。例如,flutter中有heart.png这张图

flutter:
  assets:
    - icons/heart.png
Android:

Android上通过AssetManager来获取。

  • 通过PluginRegistry.RegistrarlookupKeyForAsset来获取文件路径
  • 通过FlutterViewgetLookupKeyForAsset来获取文件路径
  • 通过AssetManageropenFd来得到文件描述符
AssetManager assetManager = registrar.context().getAssets();
String key = registrar.lookupKeyForAsset("icons/heart.png");
AssetFileDescriptor fd = assetManager.openFd(key);

IOS:

IOS上通过NSBundle来获取。

  • 通过FlutterPluginRegistrarlookupKeyForAsset来获取文件路径
  • 通过FlutterViewController.FlutterPluginRegistrar同样也可以获取文件路径
  • 通过NSBundlepathForResource来获取文件路径
NSString* key = [registrar lookupKeyForAsset:@"icons/heart.png"];
NSString* path = [[NSBundle mainBundle] pathForResource:key ofType:nil];

资料

Adding assets and images

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.02.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Flutter的资源类型
  • 指定Asset
  • Asset Variants
  • 加载Assets
  • 图片加载的适配
  • 加载图片
  • 与平台共享资源
    • Android:
      • IOS:
      • 资料
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档