如何根据屏幕密度(DPI大小)选择资产? [英] How to pick an asset according to screen density (DPI size)?

查看:103
本文介绍了如何根据屏幕密度(DPI大小)选择资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to load icons according to display density in my flutter app. how to load dynamically according to screen density(hdpi,xhdpi,xxhdpi..).

推荐答案

Flutter通过自动选择DPI相关资源来支持资产加载,请参见

Flutter supports loading of assets by automatically choosing DPI dependent resources, see https://flutter.dev/docs/development/ui/assets-and-images#resolution-aware for how the mechanism works.

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

Flutter should scale text according to devicePixelRatio value. Here is an example app showing you how that works:

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模拟器.屏幕分辨率为:

It's a modified version of the default Flutter app showing the device viewport size in pixels, the devicePixelRatio value, the size in absolute pixels. See the screenshot of the app running on Android in 3 different resolutions, and then iOS emulator with iPhone 7 Plus screen resolution. The screen resolutions are:

  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

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

The text on all devices is scaled according to the actual screen size and logical viewport.

Android 1440 x 2560,devicePixelRatio:3.5

Android 1440 x 2560, devicePixelRatio: 3.5

ets-and-images/#declaring-resolution-aware-image-assets

ets-and-images/#declaring-resolution-aware-image-assets

这篇关于如何根据屏幕密度(DPI大小)选择资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆