Use a fixed aspect ratio with the Percent Support Library 23.1



id="hist_frame" name="hist_frame2037474215" class="ss" tabindex="-1" style="height: 30px; left: -100px; position: absolute; top: -100px; width: 30px; font-family: Roboto, arial, sans-serif; font-size: 13px;">
與合適的人分享合適的內容。
首頁
反饋
幫助  ·  地區
隱私權  ·  條款  ·  地圖條款
838,725 位關注者|109,016,377 次查看
 
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 PercentFrameLayoutPercentRelativeLayout, or through any custom ViewGroup using PercentLayoutHelper(http://goo.gl/BBxu6p).
翻譯
Android Developers
232
94
Bobby Hargett的個人資料照片Benjamín Rangel的個人資料照片Tara Fennell的個人資料照片Wade Reweti的個人資料照片
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 ?
翻譯
翻譯
發表評論…

id="hist_frame" name="hist_frame2037474215" class="ss" tabindex="-1" style="height: 30px; left: -100px; position: absolute; top: -100px; width: 30px; font-family: Roboto, arial, sans-serif; font-size: 13px;">
與合適的人分享合適的內容。
首頁
首頁
個人資料
人脈
收藏集
熱門
社羣
活動
環聊
信息頁
設置
反饋
幫助  ·  地區
隱私權  ·  條款  ·  地圖條款
838,725 位關注者|109,016,377 次查看

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 PercentFrameLayoutPercentRelativeLayout, or through any custom ViewGroup using PercentLayoutHelper(http://goo.gl/BBxu6p).
翻譯
Android Developers
232
94
Bobby Hargett的個人資料照片Benjamín Rangel的個人資料照片Tara Fennell的個人資料照片Wade Reweti的個人資料照片
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.
翻譯
發表評論…

發佈了6 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章