Viewのアスペクト比を動的に設定する
ImageViewを表示する時あらかじめアス比を指定したい。 この場合はImageViewをConstraintLayout直下に入れてdimentionRatioを設定する。
<android.support.constraint.ConstraintLayout ... > <ImageView android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintDimensionRatio="h,1:1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
表示する画像のアス比がバラバラで、アス比はAPIで取得できる。constraintDimentionRatioを動的に設定したい。 その場合はConstraintSetを用いればよい。
val set = ConstraintSet() set.clone(constraintLayout) set.setDimensionRatio(R.id.image, "h,${dimensionRatio}:1") set.applyTo(constraintLayout)
ConstraintLayout.LayoutParamsにパラメータがあったけど、いじっても何も起きませんでした。