【问题标题】:Dynamic VideoView Height Based on Parent Width基于父宽度的动态 VideoView 高度
【发布时间】:2013-02-14 03:21:54
【问题描述】:

我正在尝试将 VideoView 的大小调整为父容器的宽度,然后设置高度以保持 4:3 的纵横比。我看到了一些建议扩展 VideoView 类并覆盖 onMeasure 的答案,但我不明白我得到的参数或如何使用它们:

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.VideoView;

public class MyVideoView extends VideoView {

    public MyVideoView(Context context) {
        super(context);
    }

    public MyVideoView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyVideoView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        Log.i("MyVideoView", "width="+widthMeasureSpec);
        Log.i("MyVideoView", "height="+heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

结果(在 Nexus 7 平板电脑上):

02-13 21:33:42.515: I/MyVideoView(12667): width=1073742463
02-13 21:33:42.515: I/MyVideoView(12667): height=1073742303

我正在尝试实现以下布局:

平板电脑(纵向):

  • VideoView 宽度 - 全屏或几乎全屏。
  • VideoView 高度 - 在给定宽度的情况下保持 4:3 的纵横比
  • ListView - 出现在 VideoView 下方以选择要播放的视频。

平板电脑(横向):

  • ListView - 出现在屏幕左侧,用于选择要播放的视频。
  • VideoView - 出现在屏幕右侧,应填充剩余宽度和设置的高度以保持 4:3 的纵横比。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    试试这个:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
        int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
    
                /**Adjust according to your desired ratio*/
        if (mVideoWidth > 0 && mVideoHeight > 0) {
            if (mVideoWidth * height > width * mVideoHeight) {
                // Log.i("@@@", "image too tall, correcting");
                height = (width * mVideoHeight / mVideoWidth);
            } else if (mVideoWidth * height < width * mVideoHeight) {
                // Log.i("@@@", "image too wide, correcting");
                width = (height * mVideoWidth / mVideoHeight);
            } else {
                // Log.i("@@@", "aspect ratio is correct: " +
                // width+"/"+height+"="+
                // mVideoWidth+"/"+mVideoHeight);
            }
        }
    
        setMeasuredDimension(width, height);
    
    }
    

    其中 mVideoWidth 和 mVideoHeight 是视频的当前尺寸。 希望有帮助。 :)

    【讨论】:

    • 我真的没有视频的当前尺寸,因为播放器是在创建选项卡时创建的,然后用户单击列表项来播放视频。视频可以是 640x480 或 320x240。这似乎工作正常:int width = getDefaultSize(0, widthMeasureSpec); int height = getDefaultSize(0, heightMeasureSpec); setMeasuredDimension(width, width/4*3);
    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 2017-06-01
    • 1970-01-01
    • 2014-07-05
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多