您的位置:首页 > 其它

videoview全屏播放

2016-02-23 13:41 288 查看

VideoView与视频比例缩放:

以前在论坛上也看到有人问过如何实现视频按比例缩放的问题。的确,如果仅仅使用VideoView可能达不到我们想要达到的效果。这就需要我们对VideoView做一些改动,简单的说就是另外写一个类似VideoView的类出来(庆幸Android是开源的)。我们可以很方便的获得VideoView的源代码,最简单的方法是直接在GoogleCodeSearch上找“VideoView.java”。所以重写VideoView的过程其实只是在原来的基础上进行一些修改而已,并非一个很麻烦的工作。为什么Android自带的VideoView会保持视频的长宽比而不能让我们很方便的自定义比例呢?我猜想可能Google做Android也是一个很仓促的工程,许多代码并没有考虑得太成熟。VideoView的源码中有这样一段代码:

1 @Override

2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

3 //Log.i(“@@@@“, “onMeasure”);

4 int width = getDefaultSize(mVideoWidth, widthMeasureSpec);

5 int height = getDefaultSize(mVideoHeight, heightMeasureSpec);

6 if (mVideoWidth > 0 && mVideoHeight > 0) {

7 if ( mVideoWidth * height > width * mVideoHeight ) {

8 //Log.i(“@@@“, “image too tall, correcting”);

9 height = width * mVideoHeight / mVideoWidth;

10 } else if ( mVideoWidth * height < width * mVideoHeight ) {

11 //Log.i(“@@@“, “image too wide, correcting”);

12 width = height * mVideoWidth / mVideoHeight;

13 } else {

14 //Log.i(“@@@“, “aspect ratio is correct: " +

15 //width+“/“+height+“=“+

16 //mVideoWidth+“/“+mVideoHeight);

17 }

18 }

19 //Log.i(“@@@@@@@@@@“, “setting size: " + width + 'x' + height);

20 setMeasuredDimension(width, height);

21 }

22

这就是为什么长宽比不能改变的原因了。因为在OnMeasure的时候,就对这个长宽比进行了处理。

我们把其中处理的代码屏蔽掉,视频大小就可以随着VideoView的长宽改变而改变了。

1 @Override

2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

3 //Log.i(“@@@@“, “onMeasure”);

4 int width = getDefaultSize(mVideoWidth, widthMeasureSpec);

5 int height = getDefaultSize(mVideoHeight, heightMeasureSpec);

6 /*//if (mVideoWidth > 0 && mVideoHeight > 0) {

7 if ( mVideoWidth * height > width * mVideoHeight ) {

8 //Log.i(“@@@“, “image too tall, correcting”);

9 height = width * mVideoHeight / mVideoWidth;

10 } else if ( mVideoWidth * height < width * mVideoHeight ) {

11 //Log.i(“@@@“, “image too wide, correcting”);

12 width = height * mVideoWidth / mVideoHeight;

13 } else {

14 //Log.i(“@@@“, “aspect ratio is correct: " +

15 //width+“/“+height+“=“+

16 //mVideoWidth+“/“+mVideoHeight);

17 }

18 }*/

19 //Log.i(“@@@@@@@@@@“, “setting size: " + width + 'x' + height);

20 setMeasuredDimension(width,height);

21 }

自定义VideoView

public class CustomVideoView extends VideoView {

private int mVideoWidth;
private int mVideoHeight;

public CustomVideoView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
// Log.i("@@@@", "onMeasure");

//下面的代码是让视频的播放的长宽是根据你设置的参数来决定

int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
setMeasuredDimension(width, height);
}

}

MediaController controller = new MediaController(this);

mVideoView = (CustomVideoView)findViewById(R.id.videoView1);
mVideoView.setMediaController(controller);
mVideoView.setVideoPath(mVideoPath);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: