【问题标题】:Android - VideoView inside Recyclerview match parent width and keep aspect ratioAndroid - Recyclerview 内的 VideoView 匹配父宽度并保持纵横比
【发布时间】:2021-07-19 05:11:22
【问题描述】:

这是布局的一部分:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/commentMediaContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/commentMediaLoading"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <VideoView
            android:id="@+id/commentMediaVideo"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

还有这个RecyclerView:

        holder.commentMediaVideo.setVideoPath(fileUrl)
        holder.commentMediaVideo.setOnPreparedListener { mp ->
            mp.start()
            mp.isLooping = true

            holder.commentMediaVideo.requestLayout()
            holder.commentMediaContainer.requestLayout()
        }

视频与父级匹配,但高度不正确。

如何让 VideoView 以正确的纵横比显示视频?

当我在commentMediaVideo 上尝试wrap_content 而不是0dp 时,视频根本不会显示!

【问题讨论】:

    标签: android kotlin android-recyclerview android-videoview


    【解决方案1】:

    尝试使用自定义 VideoView 并重写 onMeasure() 方法,如下所示 并在布局中使用MyVideoView

    class MyVideoView : VideoView {
        constructor(context: Context?) : super(context) 
        constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) 
        constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
        )
    
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        constructor(
            context: Context?,
            attrs: AttributeSet?,
            defStyleAttr: Int,
            defStyleRes: Int
        ) : super(context, attrs, defStyleAttr, defStyleRes)
        
    
        override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
            setMeasuredDimension(
                getDefaultSize(suggestedMinimumWidth, widthMeasureSpec),
                getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)
            )
        }
    }
    

    【讨论】:

    • 如果我理解正确,在添加这个类之后,我只需要在布局中使用
    猜你喜欢
    • 2012-11-16
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2021-11-02
    • 2015-01-28
    • 2018-10-10
    • 2019-10-31
    • 2012-06-05
    相关资源
    最近更新 更多