您的位置:首页 > 编程语言 > ASP

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

翻译

发表评论…
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: