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

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 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 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 {

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),

),

);

}

}

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:

Android 1440 x 2560, devicePixelRatio: 3.5

Android 1080 x 1920, devicePixelRatio: 2.625

Android 720 x 1280, devicePixelRatio: 1.75

iOS emulator 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

4aa78450b671d3633047032f207ca276.pngets-and-images/#declaring-resolution-aware-image-assets

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值