Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 基于父宽度的动态视频视图高度_Android_Android Layout_Android Widget - Fatal编程技术网

Android 基于父宽度的动态视频视图高度

Android 基于父宽度的动态视频视图高度,android,android-layout,android-widget,Android,Android Layout,Android Widget,我试图将视频视图的大小调整为父容器的宽度,然后设置高度以保持4:3的纵横比。我看到一些答案建议扩展VideoView类并覆盖onMeasure,但我不了解我得到的参数或如何使用它们: package com.example; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.widget.VideoView; public clas

我试图将视频视图的大小调整为父容器的宽度,然后设置高度以保持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平板电脑上):

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

平板电脑(肖像):

  • 视频视图宽度-全屏或接近全屏
  • 视频视图高度-在给定宽度的情况下,保持4:3的纵横比
  • ListView-显示在视频视图下方,用于选择要播放的视频
平板电脑(景观):

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

@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);

}
@覆盖
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
int-width=getDefaultSize(MVideWidth,widthMeasureSpec);
int height=getDefaultSize(MVideHeight、heightMeasureSpec);
/**根据您想要的比率进行调整*/
如果(mvideowitch>0&&mvideowitch>0){
如果(MVideWidth*height>width*MVideHeight){
//Log.i(“@@@@1”,“图像太高,正在更正”);
高度=(宽度*mVideoHeight/mVideoWidth);
}else if(MVideWidth*height
其中,MVideWidth和MVideHeight是视频的当前维度。
希望有帮助。:)

我没有视频的当前维度,因为创建选项卡时会创建播放器,然后用户单击列表项播放视频。视频可以是640x480或320x240。这似乎工作正常:
intwidth=getDefaultSize(0,widthMeasureSpec);int height=getDefaultSize(0,heightMeasureSpec);设置测量尺寸(宽度,宽度/4*3)
@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);

}