如何根据屏幕密度(DPI大小)选择资产?

时间:2018-12-20 07:45:55

标签: flutter flutter-layout

我正在尝试根据Flutter应用中的显示密度加载图标。如何根据屏幕密度(hdpi,xhdpi,xxhdpi ..)动态加载。

2 个答案:

答案 0 :(得分:3)

Flutter通过自动选择依赖DPI的资源来支持资产加载,有关机制的工作原理,请参见https://flutter.io/assets-and-images/#declaring-resolution-aware-image-assets

Flutter应该根据devicePixelRatio值缩放文本。这是一个示例应用程序,向您显示其工作原理:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  MediaQueryData queryData;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    queryData = MediaQuery.of(context);
    double devicePixelRatio = queryData.devicePixelRatio;
    TextStyle style38 = new TextStyle(
      inherit: true,
      fontSize: 38.0,
    );
    TextStyle style20 = new TextStyle(
      inherit: true,
      fontSize: 20.0,
    );
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          new Text(
            'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
            style: style38,
          ),
          new Text(
            'size (pixels): w=${queryData.size.width * devicePixelRatio}, h=${queryData.size.height * devicePixelRatio}',
            style: style20,
          ),
          new Text(
            'devicePixelRatio: $devicePixelRatio',
            style: style20,
          ),
          new Text(
            'size: w=${queryData.size.width}, h=${queryData.size.height}',
            style: style20,
          ),
          new Text(
            'textScaleFactor: w=${queryData.textScaleFactor}',
            style: style20,
          ),
        ],
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}

这是默认Flutter应用程序的修改版本,显示了设备视口的大小(以像素为单位),devicePixelRatio值以及以绝对像素为单位的大小。查看以3种不同分辨率在Android上运行的应用的屏幕截图,然后查看具有iPhone 7 Plus屏幕分辨率的iOS模拟器。屏幕分辨率为:

  1. Android 1440 x 2560,devicePixelRatio:3.5
  2. Android 1080 x 1920,devicePixelRatio:2.625
  3. Android 720 x 1280,devicePixelRatio:1.75
  4. iOS模拟器1080 x 1920(iPhone 7 Plus),devicePixelRatio:3.0

所有设备上的文本均根据实际屏幕尺寸和逻辑视口缩放。

Android 1440 x 2560,devicePixelRatio:3.5

enter image description here图像和图像/#declaring-resolution-aware-image-assets

答案 1 :(得分:1)

根据official documenation搜索多个小时后,抖动取决于 devicePixelRatio ,即:

Device Pixel Ratio (DPR) is the relationship between physical 
hardware-based pixels and Device-Independent Pixels which are an abstraction.

对于Android:

Denisty   Pixel Ratio
mdpi        1.0x
hdpi        1.5x
xhdpi       2.0x
xxhdpi      3.0x
xxxhdpi     4.0x

对于Ios:

enter image description here

对于Flutter:

.../image.png
.../Mx/image.png
.../Nx/image.png
...etc.

其中M和N是数字标识符,对应于其中包含的图像的标称分辨率。 换句话说,它们指定了要用于图像的设备像素比率。

假定主要资产对应于1.0的分辨率。例如,考虑图像的以下资产布局

因此,这取决于创建资产文件夹的不同子目录所需的分辨率,因此我的建议是创建以下子目录文件夹以涵盖所有DPR:

.../image.png  ---> default for android mdpi(1.0x) and ios @1x
.../1.5x/image.png  ---> default for android hdpi(1.5x)
.../2.0x/image.png  ---> default for android xhdpi(2.0x) and ios @2x
.../3.0x/image.png  ---> default for android xxhdpi(3.0x) and ios @3x
.../4.0x/image.png  ---> default for android xxxhdpi(4.0x)

在Flutter项目中:

enter image description here

在pubspec.yaml中,只需添加默认图像路径:

  assets:
    - assets/images/image.png

如果任何人需要使用SVG,只需检查以下package

SvgPicture.asset(assetName)