一般公開で共有しました  - 
 
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).
翻訳
113
39
Gabor Orosz さんのプロフィール写真Cloud Chen さんのプロフィール写真Sergio Lucas さんのプロフィール写真Sudeep Jha さんのプロフィール写真
8 件のコメント
 
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.
翻訳
 
+Ian Lake will there also be a lint update that will check for widthPercent presence and thus not complain about layoutWidth?
翻訳
 
+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!
翻訳
 
+Ian Lake cool. Looking forward to it! Keep it up with these types of posts, they're really useful.
翻訳
 
But the question now is, what is more important — maintaining a precise 16:9 aspect ratio, or having dimensions being multiples of 8dp
翻訳
 
+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.
翻訳
 
+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/)
翻訳
コメントしよう