【发布时间】:2018-03-08 11:04:58
【问题描述】:
接受 Java 和 C# (Xamarin.Android)
- 我有一个 MediaRecorder,可以将相机帧流式传输到 TextureView(基本的 camera2 方法)
- TextureView 填满了我的屏幕。
我希望视频填满屏幕但保持宽高比。
我知道我最终必须剪切视频的左/右或上/下部分,以保持纵横比。
如何使视频适合 TextureView 并“放大”直到填满整个屏幕?我不希望为了保持比率而妥协的“黑条”。
我想这必须通过覆盖 TextureView 的 OnMeasure 来完成
C# 扩展 TextureView
//Called by my MediaRecorder Implementation, when I set the video dimensions (mostly width = 1280, height = 720)
public void SetAspectRatio(int width, int height)
{
if (width == 0 || height == 0)
throw new ArgumentException("Size cannot be negative.");
mRatioWidth = width;
mRatioHeight = height;
RequestLayout();
}
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.GetSize(widthMeasureSpec);
int height = MeasureSpec.GetSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight)
{
SetMeasuredDimension(width, height);
}
else
{
if (width > (float)height * mRatioWidth / (float)mRatioHeight)
{
SetMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
}
else
{
SetMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
【问题讨论】:
-
如果你能提供一个minimal reproducible example 你迄今为止的工作,那就太棒了。
-
我添加了相应地缩放纹理视图的代码。我实际上说我的纹理视图填满了屏幕,事实并非如此。
-
我认为这篇文章为我的答案提供了一个解决方案,但它是一个更大的帖子,现在必须通过它。 stackoverflow.com/questions/17019588/…
标签: java c# fill aspect-ratio textureview