【发布时间】:2020-05-14 14:24:11
【问题描述】:
我在 CardViews 的 RecyclerView 中有一个 ex0player。 exoplayer 的高度需要根据包含的视频的高度以编程方式进行更改。此高度值通过 API 调用返回,以 像素 为单位。这是 ViewHolder 的布局:
<androidx.cardview.widget.CardView 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"
app:cardCornerRadius="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/communityPlayableCl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/toroPlayerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/transparent"
android:visibility="invisible"
app:controller_layout_id="@layout/v_player_controls"
app:keep_content_on_player_reset="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/communityPlayableIv"
app:player_layout_id="@layout/toro_exo_player_view"
app:resize_mode="zoom"
app:show_buffering="when_playing"
app:shutter_background_color="@android:color/transparent"
app:surface_type="texture_view"
app:use_controller="false" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/communityPlayableIv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="@color/black"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp"
tools:src="@color/alpha_grey" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/volumeIv"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_gravity="bottom|end"
android:padding="15dp"
android:src="@drawable/ic_volume_off"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
这是我调整高度和纵横比的方法:
if (model.mediaHeight != 0 && model.mediaHeight != null ) {
communityPlayableCl.requestLayout()
communityPlayableCl.layoutParams.height = model.mediaHeight
}
val constraintSet = ConstraintSet()
constraintSet.clone(communityPlayableCl)
constraintSet.setDimensionRatio(itemView.id,model.mediaAspectRatio)
constraintSet.applyTo(communityPlayableCl)
这会裁剪 640x640 视频的顶部和底部
【问题讨论】:
-
而不是通过布局参数更改高度,为什么不尝试更改宽高比,例如 app:layout_constraintDimensionRatio="w,
: " -
已编辑。现在它正在裁剪 640x640 视频的顶部和底部
-
您能否尝试将同一视频的硬编码纵横比设置为 1:1 并检查是否有调试目的
标签: android xml exoplayer exoplayer2.x