Nit*_*mer 28 android aspect-ratio dimensions android-videoview
我有一个像这样设置的VideoView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/player"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="@+id/video"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerVertical="true" />
<ProgressBar
android:id="@+id/loader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但VideoView匹配父容器的宽度,然后根据加载的影片的宽高比设置高度.
我想做的恰恰相反,我希望VideoView与父级的高度相匹配,同时保持宽高比不变,视频将被剪裁在两侧.
我设法拉伸VideoView来填充父级,但是不保持宽高比.
另一件事是,我将MediaController添加到VideoView,如下所示:
MediaController controllers = new MediaController(this) {
@Override
public void hide() {
if (state != State.Hidden) {
this.show();
}
else {
super.hide();
}
}
};
controllers.setAnchorView(videoView);
videoView.setMediaController(controllers);
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
controllers.show();
}
});
Run Code Online (Sandbox Code Playgroud)
这很好用,并且控制器始终保持打开状态,但在计算放置视频的位置时(因为它垂直居中),控制器的高度不会被考虑在内.
我的两个问题是:
谢谢.
ax0*_*03d 23
您应该从内置视频视图扩展.
setVideoSize
在显示视频视图之前调用,您可以从视频中提取的缩略图获取视频大小.
因此,当onMeasure
调用视频视图时,两个mVideoWidth
&mVideoHeight
均为> 0.
如果要计算控制器的高度,可以在onMeasure
方法中自行完成.
希望会有所帮助.
public class MyVideoView extends VideoView {
private int mVideoWidth;
private int mVideoHeight;
public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyVideoView(Context context) {
super(context);
}
public void setVideoSize(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Log.i("@@@", "onMeasure");
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
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);
}
}
// Log.i("@@@", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 11
我用布局解决了这个问题.看起来它固定在角落时效果很好,但却导致视频歪斜.为了测试我将相对布局的背景更改为#990000以查看红色戳穿.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative_parent"
android:background="#000000">
<VideoView
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="@+id/videoView" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
关于问题1,我很惊讶没有人提到可能使用MediaPlayer的缩放模式. https://developer.android.com/reference/android/media/MediaPlayer.html#setVideoScalingMode(int)
它有2种模式.它们都总是填满视图区域.为了在保持纵横比的同时填充空间,从而裁剪长边,需要切换到第二模式,VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
.这解决了问题的一部分.另一部分是改变VideoView的测量行为,正如其他一些答案所示.这是我这样做的方式,主要是出于懒惰而不熟悉其他人使用的元数据API,欢迎您使用此方法或其他方法之一来修复视图的大小.毯式捕获确保在mMediaPlayer存在之前调用它时的安全性,因为它可能被多次调用,并且如果字段名称发生变化,也会回退到旧行为.
class FixedSizeVideoView : VideoView {
constructor(ctx: Context) : super(ctx)
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
// rather than shrink down to fit, stay at the size requested by layout params. Let the scaling mode
// of the media player shine through. If the scaling mode on the media player is set to the one
// with cropping, you can make a player similar to AVLayerVideoGravityResizeAspectFill on iOS
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
try {
val mpField = VideoView::class.java.getDeclaredField("mMediaPlayer")
mpField.isAccessible = true
val mediaPlayer: MediaPlayer = mpField.get(this) as MediaPlayer
val width = View.getDefaultSize(mediaPlayer.videoWidth, widthMeasureSpec)
val height = View.getDefaultSize(mediaPlayer.videoHeight, heightMeasureSpec)
setMeasuredDimension(width, height)
}
catch (ex: Exception) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在布局中使用此类,只需在有机会的地方更改媒体播放器上的缩放模式.如:
video.setOnPreparedListener { mp: MediaPlayer ->
mp.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
mp.isLooping = true
mp.setScreenOnWhilePlaying(false)
}
video.start()
Run Code Online (Sandbox Code Playgroud)
使用 ConstraintLayout 我们可以实现这一点,参考下面的 xml 代码。
当 layout_width 和 layout_height 为 0dp 时,会根据其他约束动态计算 VideoView 的大小和位置。layout_constraintDimensionRatio 属性表示应用在计算VideoView 的大小时,宽高比应为3:4。此约束使视频的纵横比保持不变,并防止视图在任一方向上拉伸得太远(取决于设备的旋转方式)。
根据要求纵向/横向更改 layout_constraintDimensionRatio 值。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="3:4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
public class MyVideoView extends VideoView {
private int mVideoWidth;
private int mVideoHeight;
public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyVideoView(Context context) {
super(context);
}
@Override
public void setVideoURI(Uri uri) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(this.getContext(), uri);
mVideoWidth = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
mVideoHeight = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
super.setVideoURI(uri);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Log.i("@@@", "onMeasure");
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
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);
}
}
// Log.i("@@@", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
快速有效的修复:
无需创建从 VideoView 扩展的自定义视图。只需设置一个足够大的值即可android:layout_width
。这将设置widthSpecMode
视频视图的View.MeasureSpec.AT_MOST
,然后onMeasure()
VideoView的方法将自动调整其宽度保持比例。
<VideoView
android:id="@+id/video"
android:layout_width="2000dp"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
39894 次 |
最近记录: |