Android - 如何测量包裹在 constraintLayout 中的 'any size' 视图?
Android - How to measure a 'any size' view wrapped in a constraintLayout?
ConstraintLayout 需要两次通过才能测量任意大小的视图(尺寸为 0dp 的视图)。所以我有一个基于比率的视图,我想在用户看到该视图之前测量它的大小。如果重要的话,我正在做的是 recyclerview 的适配器。到目前为止我尝试过的是:
iv.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
width = iv.getMeasuredWidth();
height = iv.getMeasuredHeight();
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:layout_marginTop="0dp"
app:layout_constraintDimensionRatio="H,3:2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
但这总是 returns 我宽度为 1,高度为 1。我怎样才能得到实际测量值?
我需要测量视图的原因是构建一个 url。这是一个示例 url 我需要:
mywebsiteAtImgix.com/img/books/20170316171929_6b1c02a5.jpg?auto=compress&dpr=1&fit=clip&ixlib=java-1.1.1&q=75&w=1&h=1
更多信息:
我正在使用一家名为 imgix 的公司,他们会为我缩放所有图像。这非常快,而且它们缓存等,因此它对网络图像很有用。
不管怎样,请注意查询参数的宽度为 "w",高度为 "h"。现在它们以 1 的形式出现,但是对于非 constraintlayout 图像视图,当我测量它时,我得到的尺寸没问题,从 imgix 返回的图像大小合适。如果我使用 1 和 1 作为尺寸,它会带回非常大的原始图像(2mb 到 5mb +)。我需要从 constraintLayout 中获取尺寸,这样我才能正确构建 URL。希望这能解决问题。
您可以使用 ViewTreeObserver.OnPreDrawListener
在向用户显示布局之前直接获取图像的边界。
onPreDraw
boolean onPreDraw ()
Callback method to be invoked when the view tree is about to be drawn. At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs.
ConstraintLayout 需要两次通过才能测量任意大小的视图(尺寸为 0dp 的视图)。所以我有一个基于比率的视图,我想在用户看到该视图之前测量它的大小。如果重要的话,我正在做的是 recyclerview 的适配器。到目前为止我尝试过的是:
iv.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
width = iv.getMeasuredWidth();
height = iv.getMeasuredHeight();
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:layout_marginTop="0dp"
app:layout_constraintDimensionRatio="H,3:2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
但这总是 returns 我宽度为 1,高度为 1。我怎样才能得到实际测量值?
我需要测量视图的原因是构建一个 url。这是一个示例 url 我需要:
mywebsiteAtImgix.com/img/books/20170316171929_6b1c02a5.jpg?auto=compress&dpr=1&fit=clip&ixlib=java-1.1.1&q=75&w=1&h=1
更多信息:
我正在使用一家名为 imgix 的公司,他们会为我缩放所有图像。这非常快,而且它们缓存等,因此它对网络图像很有用。
不管怎样,请注意查询参数的宽度为 "w",高度为 "h"。现在它们以 1 的形式出现,但是对于非 constraintlayout 图像视图,当我测量它时,我得到的尺寸没问题,从 imgix 返回的图像大小合适。如果我使用 1 和 1 作为尺寸,它会带回非常大的原始图像(2mb 到 5mb +)。我需要从 constraintLayout 中获取尺寸,这样我才能正确构建 URL。希望这能解决问题。
您可以使用 ViewTreeObserver.OnPreDrawListener
在向用户显示布局之前直接获取图像的边界。
onPreDraw
boolean onPreDraw ()
Callback method to be invoked when the view tree is about to be drawn. At this point, all views in the tree have been measured and given a frame. Clients can use this to adjust their scroll bounds or even to request a new layout before drawing occurs.