【问题标题】:Expand card view height to height of contained exoplayer, crop only left and right sides of expoplayer将卡片视图高度扩展到包含的 exoplayer 的高度,仅裁剪 expoplayer 的左侧和右侧
【发布时间】: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


【解决方案1】:

您可以使用类似的方法从视频中获取尺寸。我也会在 Uri.parse 周围添加一个 try/catch,如果无法解析文件,它将失败。

Pair<Integer, Integer> getMediaDimensions(File file, Context context) {
    if (!file.exists()) return null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(context, Uri.parse(file.getAbsolutePath()));
    int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
    int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
    retriever.release();

    return new Pair<>(width, height);
}

从那里,您可以在计算比率后手动计算和设置新的高度/宽度。或者,使用 ConstraintLayout 作为播放器的容器,并将其 layout_constraintDimensionRatio 设置为 H,1:1,然后将每个 1 替换为您的宽度/高度。

如果您使用的是 Kotlin,可以关注 this post 进行相应调整,并添加可重复使用的扩展。

【讨论】:

  • 编辑了我的帖子。纵横比和尺寸随模型一起传递。但是,仍然没有正确显示。
  • model.mediaAspectRatio 是一个有效的字符串吗?您应该只对预期的内容进行硬编码并首先对其进行测试。另外,为什么要设置 ConstraintLayout 的高度?删除行强制布局高度
  • 是的,我正在打印出来。我也用硬编码值进行了测试。如果我不以编程方式更改布局高度,每个视图都会崩溃。
  • 您是否尝试添加 app:layout_constraintDimensionRatio="H,3:1" 或类似于 XML 的内容?它不应该崩溃。您还应该在 toroPlayerView.id 上设置 setDimensionRatio。也许还可以在约束集更改后尝试 requestLayout。
【解决方案2】:

我最终使用传递的纵横比手动设置视图的尺寸

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多