在android的VideoVideo中缩放视频 [英] Scale video in android's VideoVideo
问题描述
我知道如何拉伸ViewVideo以适合屏幕. 但是MX Player允许将视频放大. 视频实际上比屏幕大. 这样观众就可以看到视频的裁剪部分.
I know how to strech ViewVideo to fit screen. But MX Player permits to make video bigger. Video is virtually bigger then screen. So viewer will see cropped part of video.
Android的VideoView是否有可能做到这一点?
Is it possible with android's VideoView to make such a trick?
推荐答案
我不认为您可以仅使用常规的VideoView
来完成此任务,尽管我可以使用更复杂的方法来做到这一点.
I dont think you can do it with just a regular VideoView
... although I was able to do that with a bit more complicated approach.
TLDR:您需要创建一个自定义视图,该视图的行为类似于VideoView,但基于TextureView而不是SurfaceView,并为此视图设置适当的变换矩阵.
TLDR请先查看我的答案的编辑内容,因为您实际上可以使用VideoView进行编辑...
创建此视图有点棘手,主要是因为MediaPlayer
事件可以在不同设备上播放不同视频时以不同顺序触发.
Creating this view is a bit tricky mostly due to the fact that MediaPlayer
events can be triggered in different order on different devices and when playing different videos.
基本思想是:
- 创建扩展
TextureView
的自定义 - 保留一个
MediaPlayer
对象,该对象将管理整个播放中的视频内容...例如视图的字段 - 通过
setSurfaceTextureListener()
设置视图的TextureListener
- 内部
TextureListener
方法(更具体的是内部onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height)
)使用构造函数创建新的Surface
:
View
- create a custom
View
that extendsTextureView
- keep a
MediaPlayer
object that would manage whole playing video thing... as for example a field of the view - set your view's
TextureListener
withsetSurfaceTextureListener()
- inside
TextureListener
methods (insideonSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height)
to be more specific) create a newSurface
with constructor:
Surface surface = new Surface(surfaceTexture);
- 并使用以下命令将此表面设置为
MediaPlayer
的表面: - and set this surface as a surface of the
MediaPlayer
with:
mMediaPlayer.setSurface(surface);
这个变换矩阵的创建可能像这样(示例来自我的旧代码,如果我没记错的话,它是将视频调整为适合视图的高度和裁剪剩下的所有内容):
And the creation of this transform matrix could look like this (the example is from the old code of mine and if I remember correctly it was fitting the video to the height of the view and cropping everything that was left on sides):
float scaleY = 1.0f;
float scaleX = (mVideoWidth * mViewHeight / mVideoHeight) / mViewWidth;
int pivotPointX = (int) (mViewWidth / 2);
int pivotPointY = (int) (mViewHeight / 2);
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);
setTransform(matrix);
好吧...当我再考虑一次时,也许您实际上可以只用常规的VideoView
来完成该操作.只需遵循类似的方法进行scaleX
,scaleY
,pivotPointX
和pivotPointY
计算,并使用方法setScaleX()
,setScaleY()
,SurfaceView
设置这些值. >和setPivotY()
,然后查看是否有效.如果您进行测试,请告诉我是否可行.
Well... when I think about it one more time, maybe you could actually do that with just a regular VideoView
. Just follow the similar approach with the scaleX
,scaleY
,pivotPointX
and pivotPointY
calculation and set those values for the underlying SurfaceView
of the VideoView
with methods setScaleX()
, setScaleY()
, setPivotX()
and setPivotY()
and see if it works. And let me know if it works if you test it.
这篇关于在android的VideoVideo中缩放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!