固定纵横比视图
我将如何实现固定宽高比View
?我希望在 GridView
中包含宽高比为 1:1 的项目。我认为子类化子类比 GridView 更好?
编辑:我认为这需要以编程方式完成,这没问题。另外,我不想限制大小,只想限制宽高比。
How would I go implementing a fixed aspect ratio View
? I'd like to have items with 1:1 aspect ratio in a GridView
. I think it's better to subclass the children than the GridView
?
EDIT: I assume this needs to be done programmatically, that's no problem. Also, I don't want to limit the size, only the aspect ratio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我实现了FixedAspectRatioFrameLayout,因此我可以重用它并使任何托管视图具有固定的宽高比:
I implemented FixedAspectRatioFrameLayout, so I can reuse it and have any hosted view be with fixed aspect ratio:
为了不使用第三方解决方案,并考虑到 PercentFrameLayout 和 PercentRelativeLayout 在 26.0.0 中已被弃用,我建议您考虑使用 ConstraintLayout 作为网格项的根布局。
您的
item_grid.xml
可能如下所示:因此,您会得到如下所示的内容:
< img src="https://i.sstatic.net/Pbb2E.png" alt="固定比例大小的网格项目">
To not use third-party solution and considering the fact that both PercentFrameLayout and PercentRelativeLayout were deprecated in 26.0.0, I'd suggest you to consider using ConstraintLayout as a root layout for your grid items.
Your
item_grid.xml
might look like:As a result you get something like this:
对于新用户,这里有一个更好的非代码解决方案:
一个名为 的新支持库百分比支持库在Android SDK v22中可用(MinAPI是7,我认为,不确定):
要包含此内容,请将其添加到您的build.gradle em> :
现在用
PercentRelativeLayout
/PercentFrameLayout
:您可以查看示例 这里。
For new users, here's a better non-code solution :
A new support library called Percent Support Library is available in Android SDK v22 (MinAPI is 7 me thinks, not sure) :
To include add this to your build.gradle :
Now wrap your view (the one that needs to be square) with a
PercentRelativeLayout
/PercentFrameLayout
:You can see an example here.
我最近为这个问题创建了一个辅助类,并编写了
代码的主要内容如下:
I recently made a helper class for this very problem and wrote a blog post about it.
The meat of the code is as follows:
我使用 TalL 的答案创建了一个布局库。请随意使用它。
RatioLayouts
安装
将其添加到文件顶部
用法
在布局中的根视图上定义“app”命名空间
将此库包含在您的布局
更新
中,引入 ConstraintLayout 您不必编写一行代码或使用第三方或依赖于 26.0.0 中已弃用的
PercentFrameLayout
。以下示例展示了如何使用
ConstraintLayout
保持布局的宽高比为 1:1:I created a layout library using TalL's answer. Feel free to use it.
RatioLayouts
Installation
Add this to the top of the file
Usage
Define 'app' namespace on root view in your layout
Include this library in your layout
Update
With introduction of ConstraintLayout you don't have to write either a single line of code or use third-parties or rely on
PercentFrameLayout
which were deprecated in 26.0.0.Here's the example of how to keep 1:1 aspect ratio for your layout using
ConstraintLayout
:我使用并喜欢 Jake Wharton 的
ImageView
实现(其他视图也应该类似),其他人可能也会喜欢它:AspectRatioImageView.java - 尊重应用于特定测量的宽高比的 ImageView
很好,它可以在 xml 中设置样式 已经。
I've used and liked Jake Wharton's implementation of
ImageView
(should go similarly for other views), others might enjoy it too:AspectRatioImageView.java - ImageView that respects an aspect ratio applied to a specific measurement
Nice thing it's styleable in xml already.
您可能会找到第三方库。不要使用它们,而是使用约束布局。
下面的代码将 ImageView 的宽高比设置为 16:9,无论屏幕尺寸和方向如何。
app:layout_constraintDimensionRatio="H,16:9"
。这里,高度是相对于布局的宽度设置的。对于您的问题,请设置
android:layout_width="match_parent"
并使用 < code>app:layout_constraintDimensionRatio="H,1:1" 在您的视图中。You may find third-party libraries. Instead of using them, use constraint layout.
Below code sets the aspect ratio of ImageView as 16:9 regardless of the screen size and orientation.
app:layout_constraintDimensionRatio="H,16:9"
. Here, height is set with respect to the width of the layout.For your question, set
android:layout_width="match_parent"
and useapp:layout_constraintDimensionRatio="H,1:1"
in your view.只需覆盖
onSizeChanged
并计算比率即可。长宽比的公式是:
这会给你类似的东西:
希望这有帮助!
Simply override
onSizeChanged
and calculate ratio there.Formula for aspect ratio is:
this would give you something like that:
hope this helps!
Google 的 ExoPlayer 附带
AspectRatioFrameLayout< /code>
如下所示:
然后,您必须以编程方式设置宽高比:
请注意,您还可以使用
setResizeMode
< /a>.由于您显然不会获取这个类的整个 ExoPlayer 库,因此您可以简单地将文件从 GitHub 复制粘贴到您的项目(它是开源的):
https://github.com/google/ExoPlayer/blob/release-v2/library/ui/src/main/java/com/google/android/exoplayer2/ui/AspectRatioFrameLayout.java
不要忘记也获取属性
resize_mode
:https: //github.com/google/ExoPlayer/blob/release-v2/library/ui/src/main/res/values/attrs.xml#L18-L25
The ExoPlayer from Google comes with an
AspectRatioFrameLayout
that you use like this:Then you must set the aspect ratio programmatically:
Note that you can also set the resize mode programmatically with
setResizeMode
.Since you are obviously not going to grab the whole ExoPlayer library for this single class, you can simply copy-paste the file from GitHub to your project (it's open source):
https://github.com/google/ExoPlayer/blob/release-v2/library/ui/src/main/java/com/google/android/exoplayer2/ui/AspectRatioFrameLayout.java
Don't forget to grab the attribute
resize_mode
too:https://github.com/google/ExoPlayer/blob/release-v2/library/ui/src/main/res/values/attrs.xml#L18-L25
我曾经用过这种方式。
I have used in this way.
<androidx.constraintlayout.widget.ConstraintLayout