Use a fixed aspect ratio with the Percent Support Library 23.1
2015-11-16 11:44
721 查看
加入 Google+与合适的人分享合适的内容。首页首页个人资料人脉收藏集热门社群活动环聊信息页设置反馈帮助 · 地区隐私权 · 条款 · 地图条款
Android Developers关注838,725 位关注者|109,016,377 次查看Android Development Patterns
Android Developers
公开分享 - 2015年11月5日 Use a fixed aspect ratio with the Percent Support Library 23.1Pro-tip by +Ian Lake
The Percent Support Library (https://goo.gl/KbnO7W) makes it easy to set dimensions and margins in terms of a percentage of the overall space. In the 23.1 release (http://goo.gl/Ohd5Sy), it also gained the ability to set a custom aspect ratio via app:layout_aspectRatio.
This allows you to set only a single dimension, such as only the width, and the height will be automatically determined based on the aspect ratio you’ve defined, whether it is 4:3 or 16:9 or even a square 1:1 aspect ratio.
So building a navigation drawer header with a 16:9 background image would look like:
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%"
android:scaleType="centerCrop"
android:src="@drawable/header_background"/>
<!-- The rest of your layout -->
</android.support.percent.PercentRelativeLayout>
You’ll note we use layout_widthPercent instead of layout_width - this ensures that the height is not erroneously set to 0dp before the first layout pass is done and ensures the aspect ratio is always correctly computed based on the current width.
So how did a 16:9 aspect ratio turn into 178%? Our target 16:9 aspect ratio can also be expressed as a 1.78:1 ratio or equivalently, a width 178% of the height. This is the format the layout_aspectRatio expects.
Of course, you can also define the aspect ratio in separate XML files with code such as:
<item name="header_aspectRatio" type="fraction">178%</item>
This makes it possible to change or reuse them across different form factors or layouts.
Material design designates a number of ratio keylines (http://goo.gl/OHeq6x) which you can use in your app, but you could also consider using this for list items (where you may be using ?android:attr/listPreferredItemHeight) with items such as a profile image or video thumbnail for a fixed aspect ratio.
You’ll be able to use this with PercentFrameLayout, PercentRelativeLayout, or through any custom ViewGroup using PercentLayoutHelper(http://goo.gl/BBxu6p).翻译Support Library FeaturesAndroid Developers23294
11 条评论
Ian Lake2015年11月5日+12
3
2
Note: you’ll find Android Studio may complain about missing layout_width or layout_height. Feel free to ignore it or add <!-- suppress AndroidDomInspection --> to suppress it entirely.翻译
Bogdan Zurac2015年11月5日+1
2
1
+Ian Lake will there also be a lint update that will check for widthPercent presence and thus not complain about layoutWidth?翻译
Ian Lake2015年11月5日+3
4
3
+Bogdan Zurac - that's the plan! There's really two Lint checks that need to be added - allowing widthPercent/heightPercent to be valid substitutes for width/height and allowing aspectRatio to substitute for the missing dimension (i.e., declaring widthPercent and aspectRatio makes heightPercent/height unnecessary, etc).
There's also some work going on with being able to package lint rules with libraries (which would make sense in this case as it is a unique rule for the Percent Support Library) which may delay the availability of the Lint rules until that is completely finalized.
I'll be sure to mention it when it comes out (either as part of a Support Library blog post or another pro-tip) though!翻译
Bogdan Zurac2015年11月5日+2
3
2
+Ian Lake cool. Looking forward to it! Keep it up with these types of posts, they're really useful.翻译
Paul Danyliuk (Actine)2015年11月5日
But the question now is, what is more important — maintaining a precise 16:9 aspect ratio, or having dimensions being multiples of 8dp翻译
Ian Lake2015年11月5日+1
2
1
+Paul Danyliuk - generally, and is the case on the material design guidelines on aspect ratio, it is the width that is in 8dp multiples and the height that could be aspect ratio determined.翻译
Paul Danyliuk (Actine)2015年11月5日
+Ian Lake OK, thanks.
Not related to this post, another question for you. I cannot find vector drawable support library. I know it wasn't officially released yet, but I remember seeing it in Google Git a while ago, and I kinda need it now :( Was it hidden/deleted altogether from the repo and all versions and tags?|
Edit: nvm, found it under the new name (platform/frameworks/support/+/master/graphics/drawable/)翻译
Said Tahsin Dane2015年11月5日
OMG! This is so helpful. I've been doing this with custom views. Now it is possible with only 1 line of xml. 翻译
刘慧斌2015年11月9日
nice!much better翻译
amjed baig2015年11月9日
+Ian Lake I am using in this as an item of Recycler view, It never works..!! Is it not built to support in recycler view ?翻译
Ian Lake2015年11月9日
+amjed baig - it's working great here. Best to post a stackoverflow.comquestion with your code and layout.翻译发表评论…
登录
加入 Google+
与合适的人分享合适的内容。
首页
首页
个人资料
人脉
收藏集
热门
社群
活动
环聊
信息页
设置
反馈
帮助 · 地区
隐私权 · 条款 · 地图条款
Android Developers
关注
838,725 位关注者|109,016,377 次查看
Android Development Patterns
Android Developers
公开分享 - 2015年11月5日Use a fixed aspect ratio with the Percent Support Library 23.1
Pro-tip by +Ian
Lake
The Percent Support Library (https://goo.gl/KbnO7W) makes it easy to set dimensions and margins
in terms of a percentage of the overall space. In the 23.1 release (http://goo.gl/Ohd5Sy),
it also gained the ability to set a custom aspect ratio via app:layout_aspectRatio.
This allows you to set only a single dimension, such as only the width, and the height will be automatically determined based on the aspect ratio you’ve defined, whether it is 4:3 or 16:9 or even a square 1:1 aspect ratio.
So building a navigation drawer header with a 16:9 background image would look like:
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%"
android:scaleType="centerCrop"
android:src="@drawable/header_background"/>
<!-- The rest of your layout -->
</android.support.percent.PercentRelativeLayout>
You’ll note we use layout_widthPercent instead of layout_width - this ensures that the height is not erroneously set to 0dp before the first layout pass is done and ensures the aspect ratio is always correctly computed based on the
current width.
So how did a 16:9 aspect ratio turn into 178%? Our target 16:9 aspect ratio can also be expressed as a 1.78:1 ratio or equivalently, a width 178% of the height. This is the format the layout_aspectRatio expects.
Of course, you can also define the aspect ratio in separate XML files with code such as:
<item name="header_aspectRatio" type="fraction">178%</item>
This makes it possible to change or reuse them across different form factors or layouts.
Material design designates a number of ratio keylines (http://goo.gl/OHeq6x) which you can
use in your app, but you could also consider using this for list items (where you may be using ?android:attr/listPreferredItemHeight) with items such as a profile image or video thumbnail for a fixed aspect ratio.
You’ll be able to use this with PercentFrameLayout, PercentRelativeLayout, or through any custom ViewGroup using PercentLayoutHelper(http://goo.gl/BBxu6p).
翻译
Support
Library Features
Android Developers
232
94
11 条评论
Ian Lake
2015年11月5日
+
1
2
3
2
Note: you’ll find Android Studio may complain about missing layout_width or layout_height. Feel free to ignore it or add <!-- suppress AndroidDomInspection --> to suppress it entirely.
翻译
Bogdan Zurac
2015年11月5日
+
1
2
1
+Ian
Lake will there also be a lint update that will check for widthPercent presence and thus not complain about layoutWidth?
翻译
Ian Lake
2015年11月5日
+
3
4
3
+Bogdan
Zurac - that's the plan! There's really two Lint checks that need to be added - allowing widthPercent/heightPercent to be valid substitutes for width/height and allowing aspectRatio to substitute for the missing dimension (i.e.,
declaring widthPercent and aspectRatio makes heightPercent/height unnecessary, etc).
There's also some work going on with being able to package lint rules with libraries (which would make sense in this case as it is a unique rule for the Percent Support Library) which may delay the availability of the Lint rules until that is completely finalized.
I'll be sure to mention it when it comes out (either as part of a Support Library blog post or another pro-tip) though!
翻译
Bogdan Zurac
2015年11月5日
+
2
3
2
+Ian
Lake cool. Looking forward to it! Keep it up with these types of posts, they're really useful.
翻译
Paul Danyliuk (Actine)
2015年11月5日
But the question now is, what is more important — maintaining a precise 16:9 aspect ratio, or having dimensions being multiples of 8dp
翻译
Ian Lake
2015年11月5日
+
1
2
1
+Paul
Danyliuk - generally, and is the case on the material design guidelines on aspect ratio, it is the width that is in 8dp multiples and the height that could be aspect ratio determined.
翻译
Paul Danyliuk (Actine)
2015年11月5日
+Ian
Lake OK, thanks.
Not related to this post, another question for you. I cannot find vector drawable support library. I know it wasn't officially released yet, but I remember seeing it in Google Git a while ago, and I kinda need it now :( Was it hidden/deleted altogether from
the repo and all versions and tags?|
Edit: nvm, found it under the new name (platform/frameworks/support/+/master/graphics/drawable/)
翻译
Said Tahsin Dane
2015年11月5日
OMG! This is so helpful. I've been doing this with custom views. Now it is possible with only 1 line of xml.
翻译
刘慧斌
2015年11月9日
nice!much better
翻译
amjed baig
2015年11月9日
+Ian
Lake I am using in this as an item of Recycler view, It never works..!! Is it not built to support in recycler view ?
翻译
Ian Lake
2015年11月9日
+amjed
baig - it's working great here. Best to post a stackoverflow.comquestion
with your code and layout.
翻译
发表评论…
相关文章推荐
- 微软发布了ASP.NET WebHooks预览版
- ASP.NET 大文件上传
- 确定了,准备学习安卓和asp.net
- ASP.NET图片上传实例(附源码)
- ASP.NET验证码实现(附源码)
- ASP.NET登录控件login。
- ASP.NET 常用状态(信息)保存方式
- Asp.Net跨平台
- 无法识别的属性“type”+IIS没有Asp.net配置选项的解决方法
- Asp.net压缩网站中的文件
- ASP+Access UTF-8 网页乱码问题解决办法
- 无法在web服务器上启动调试。未能启动asp.net调试。在不调试的情况下启动项目也许能获得更多信息。
- Asp.Net跨平台:Ubuntu14.0+Mono+Jexus+Asp.Net
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
- G1垃圾收集 永久代和MetaSpace
- asp.net 中GridView控件实现全选及反选的功能
- asp.net 中GridView控件实现全选及反选的功能
- 手脱ASPack v2.12变形壳2
- 手脱Aspack变形壳1
- 【Asphyre引擎】学习笔记(二)