-
Notifications
You must be signed in to change notification settings - Fork 3.4k
doc for resolution aware images #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| --- | ||
| layout: page | ||
| title: Resolution Aware Images | ||
| permalink: /resolution-aware-images/ | ||
| --- | ||
|
|
||
| Flutter can load resolution-appropriate images for the current device | ||
| pixel ratio. | ||
|
|
||
| AssetImage understands how to map a logical requested asset onto one that most | ||
| closely matches the current device pixel ratio. In order for this mapping to | ||
| work, assets should be arranged according to a particular directory structure: | ||
|
|
||
| .../image.png | ||
| .../Mx/image.png | ||
| .../Nx/image.png | ||
| ...etc. | ||
|
|
||
|
|
||
| Where M and N are numeric identifiers that correspond to the nominal resolution | ||
| of the images contained within. The main asset is assumed to correspond to a | ||
| resolution of 1.0. For example, consider the following asset layout for an | ||
| image named `my_icon.png`: | ||
|
|
||
| .../my_icon.png | ||
| .../2.0x/my_icon.png | ||
| .../3.0x/my_icon.png | ||
|
|
||
| *Tip*: Remember to declare your resources inside `flutter.yaml`, | ||
| as shown in the [Tutorial](/tutorial/). Flutter's build phase automatically | ||
| picks up any asset variants in subfolders that have the same name as a main | ||
| asset. For example, if you declare `my_icon.png` in `flutter.yaml`, | ||
| the build phase | ||
| automatically includes `2.0x/my_icon.png` and `3.0x/my_icon.png` in the | ||
| asset bundle. | ||
|
|
||
| On devices with a device pixel ratio of 1.8, the asset `.../2.0x/my_icon.png` | ||
| would be chosen. For a device pixel ratio of 2.7, the asset | ||
| `.../3.0x/my_icon.png` would be chosen. | ||
|
|
||
| If the width and height of the rendered image are not specified, the nominal | ||
| resolution is used to scale the asset so that it will occupy the same amount | ||
| of screen space as the main asset would have, just with a higher resolution. | ||
| That is, if `.../my_icon.png` is 72px by 72px, then `.../3.0x/my_icon.png` | ||
| should be 216px by 216px; but they both will render into 72px by 72px | ||
| (in logical pixels) if width and height are not specified. | ||
|
|
||
| The way this works is through an object called AssetVendor established at the | ||
| top of the build tree. AssetVendor replaces the default asset bundle, so | ||
| really anything using the default asset bundle will inherit resolution awareness | ||
| when loading images. If you work with some of the lower level classes, like | ||
| ImageResource or ImageCache, you'll also notice parameters related | ||
| to scale. | ||
|
|
||
| Some caveats: | ||
|
|
||
| * If you're not using MaterialApp in your application, and you want to use | ||
| resolution awareness, you'll need to establish your own AssetVendor in your | ||
| build logic. (This may change, please see | ||
| [this issue](https://github.com/flutter/flutter/issues/1346).) | ||
| * If for some reason you establish a custom MediaQuery or DefaultAssetBundle | ||
| farther down in the widget hierarchy, the top-level AssetVendor won't be aware | ||
| of the change. If you want resolution awareness with the new MediaQuery or | ||
| DefaultAssetBundle you specify, you'll need to create an AssetVendor at that | ||
| point in the tree as well. | ||
|
|
||
| You can see an example | ||
| ([examples/widgets.dart](https://github.com/flutter/flutter/tree/master/examples/widgets)) | ||
| from the flutter repo. | ||
| Run `flutter start -t resolution_awareness.dart` to see it in action. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may change this soon, see flutter/flutter#1346.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Added.